I have two date string in ISO format. I am calculating the minutes difference between them using moment js.
var currenttime = new Date().toISOString();
var expiretime = '2020-06-05T12:18:33.000Z';
let minutes = moment(expiretime, 'YYYY-MM-DD[T]HH:mm:ss. SSS[Z]').diff(moment(currenttime, 'YYYY-MM-DD[T]HH:mm:ss. SSS[Z]'), 'minutes');
console.log(minutes)
Now i have two questions
Is this is the best way to calculate the minutes difference ?
When i run new Date().toISOString()
the value before z
is the supposed to be the timezone but on every restart it changes ?
Please let me know what is the issue ?
The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ.
To get the time difference between 2 date times, we can use the diff method from moment.js We call diff to get the difference of laterDateTime and earlierDateTime and convert the difference to UTC. This will output the difference in HH:mm:ss format. We get ‘02:20:30' as the value of diff .
The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific. Independent of input format, JavaScript will (by default) output dates in full text string format: ISO 8601 is the international standard for the representation of dates and times.
The Date object in JavaScript manages time data using absolute numbers, such as Unix time, internally. Constructors and methods like parse (),getHour (),setHour (). However, it’s important to note that it is influenced by the client’s local time zone (the time zone of the OS running the browser, to be exact).
Time Zones. When setting a date, without specifying the time zone, JavaScript will use the browser's time zone. When getting a date, without specifying the time zone, the result is converted to the browser's time zone. In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to CDT...
I don't think you have to include moment for this purpose only, this can be done in ordinary Javascript.
To calculate time difference just subtract the two timestamps which will give the difference in milliseconds. Then divide by 60*1000
which will give the time difference in minutes.
let currentTime = new Date();
let expireTime = new Date('2020-06-05T12:18:33.000Z');
let minutes = (expireTime - currentTime) / (1000 * 60);
console.log(minutes);
For your first question, what you can do is simply subtract the two dates (date objects and not strings) ,
Newer date - Older date ( this will give the result in milliseconds)
or use Math.abs() function to calculate milliseconds -
var msec = Math.abs( currenttime - expiretime );
and convert this to minutes using -
var min = Math.floor((msec/1000)/60);
2) Z in the datetime string stands for Zulu, it indicates that the time is in UTC format. You can simply subtract two date objects.
var currenttime = new Date();
var currenttime_UTC = currenttime.getUTCDate();
var expiretime = new Date('2020-06-05T12:18:33.000Z');
var msec = Math.abs( currenttime_UTC - expiretime );
var min = Math.floor((msec/1000)/60);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With