I have a datepicker which returns local time in the format dddd, MMMM DO YYYY, h:mm:ss a to a variable localTime. I have an option to change the timezone from a <select> which contains timezone names from moment.tz.names(). When timezone changes, I need to change the timezone of localTime to new timezone without changing the time.How can I do this with moment.js
let timeString = "Wednesday, January 10th 2018, 9:10:19 pm";
let localTime = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').format('YYYY-MM-DD HH:mm:ss ZZ');
let localTimeInUtc = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').utc().format('YYYY-MM-DD HH:mm:ss ZZ');
let newTimezone = "EST";
let newTime = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').tz(newTimezone).format('YYYY-MM-DD HH:mm:ss ZZ');
let newTimeInUtc = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').tz(newTimezone).utc().format('YYYY-MM-DD HH:mm:ss ZZ');
console.log(localTime);
console.log(localTimeInUtc);
console.log(newTime);
console.log(newTimeInUtc);
Here time in UTC is same for localTime and newTime
According to Docs
Maybe you are looking for format2 in the following code.
format1 - creates moment and sets the time-zone (will not update the offset).
format2 - creates the moment with the given timezone.
var date = moment().format("dddd, MMMM D YYYY, h:mm:ss a"), format = "dddd, MMMM D YYYY, h:mm:ss a", timezone = "America/New_York";
var format1 = moment(date,format).tz(timezone);
var format2 = moment.tz(date, format,timezone);
console.log(format1.format(format)+" -> "+format1.format());
console.log(format2.format(format)+" -> "+format2.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
See this Github issue once.
Related
How can I calculate the difference between two dates time in moment.js?
moment.duration((moment(startDate).format("MM-DD-YYYY h:mm:ss a")).diff(moment(new Date()).format("MM-DD-YYYY h:mm:ss a")));
and My startDate = 2022-02-02T12:12:25Z
I found the solution ,may help other people :
var now = moment(new Date(), "DD/MM/YYYY HH:mm:ss");
var then = moment(new Date(startDate), "DD/MM/YYYY HH:mm:ss");
var diffTime = moment.duration(then.diff(now));
and it depends to you how you want to get difference in seconds ,minutes or hours :
diffTime.asHours();
const moment = require('moment');
var format_1 = 'dddd, MMMM DD, yyyy';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true
var format_2 = 'dddd, MMMM DD, yyyy';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns false
The first console.log() returns like true, as expected. Proving any date in the year 2020 the result of the isValid() function is false for this specific time format. Thanks for any help :)
Year is represented as YYYY as per moment.js docs
Modified your existing code to get the desirable output below :
var format_1 = 'dddd, MMMM DD, YYYY';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true
var format_2 = 'dddd, MMMM DD, YYYY';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns true as well
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
I'm trying to present a Date inside ng-grid, I wrote a filter either the date is Expired or not, if so present a text otherwise a Date format MMM dd yyyy, hh:mm a
.filter("expiredDate", Filter);
function Filter($filter) {
return function (val) {
if (val === 'Expired')
return val;
var res = $filter('date')(val, 'MMM dd yyyy, hh:mm a');
return res;
}
}
This displays "/Date(1495366770000)/" instead of the Date and this is the value of val
I am using bootstrap datetimepicker have to get the datetime and add days to it
var get_date_time = Friday, October 23rd 2015, 12:00:00 am
format get_date_time to oct 25 2015 12:00:00 am how to format
How to format this to
var d = new Date('oct 25 2015 12:00:00 am');
d.setDate(d.getDate() + 2)
alert(d);
output should be
example
Tuesday, October 27th 2015, 12:00:00 am
How to achieve this?
You need to do the following...
Get your date var d = new Date('Oct 25 2015 12:00:00 am').
Then use d.getTime() to get total time in milliseconds. Then for each day you wish to add, add 3600000 to it. Then create a new date from that var newDate = new Date(1445758146777).
var d = new Date('Oct 25 2015 12:00:00 am');
var milisecs = d.getTime();
var oneDay = 3600000;
// Add one day to the date...
milisecs += oneDay;
var newDate = new Date(milisecs)
Then format it accordingly :-)
I have used moment.js http: //momentjs.com/ to convert particular format
var get_date_time = Friday, October 23rd 2015, 12:00:00 am
using moment js i have formatted to date like the below
alert(moment(get_date_from,'dddd, MMMM Do YYYY, h:mm:ss a').format("MMM DD YYYY h:mm:ss a"));
then i have followed like this to add number of days calculation
var date1 = deal_from_date.setDate(deal_from_date.getDate() + 2);
I have the following code:
23/09/2014 - 09:15 (DD/MM/YYYY - HH:SS)
I need to use JS to reformat in milliseconds.
var dateString = "23/09/2014 - 09:15",
dateArgs = dateString.match(/\d{2,4}/g),
year = dateArgs[2],
month = parseInt(dateArgs[1]) - 1,
day = dateArgs[0],
hour = dateArgs[3],
minutes = dateArgs[4];
var milliseconds = new Date(year, month, day, hour, minutes).getTime();
you can use Moment.js See http://jsfiddle.net/mendesjuan/jpkkngjw/
// getTime() returns milliseconds since epoch
moment("23/09/2014 - 09:15", "DD/MM/YYYY - HH:SS").toDate().getTime()
Here are some other examples of using Momento
moment().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA"); // "Sun, 3PM"
moment('gibberish').format('YYYY MM DD'); // "Invalid date"