Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating time difference in iso time format in javascript?

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

  1. Is this is the best way to calculate the minutes difference ?

  2. 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 ?

like image 650
TechChain Avatar asked Jun 06 '20 06:06

TechChain


People also ask

What is ISO date format in JavaScript?

The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ.

How to get the time difference between 2 date times in JavaScript?

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 .

What is the ISO format for dates in JavaScript?

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.

How does date work in JavaScript?

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).

What are the time zones used by JavaScript?

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...


2 Answers

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);
like image 76
dreamHatX Avatar answered Oct 23 '22 22:10

dreamHatX


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);
like image 2
Parth Kapadia Avatar answered Oct 23 '22 22:10

Parth Kapadia