Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 ISO 8601 timestamps and output seconds/minutes difference

Tags:

I need to write JavaScript that's going to allow me to compare two ISO timestamps and then print out the difference between them, for example: "32 seconds".

Below is a function I found on Stack Overflow, it turns an ordinary date into an ISO formatted one. So, that's the first thing out the way, getting the current time in ISO format.

The next thing I need to do is get another ISO timestamp to compare it with, well, I have that stored in an object. It can be accessed like this: marker.timestamp (as shown in the code below). Now I need to compare those two two timestamps and work out the difference between them. If it's < 60 seconds, it should output in seconds, if it's > 60 seconds, it should output 1 minute and 12 seconds ago for example.

Thanks!

function ISODateString(d){  function pad(n){return n<10 ? '0'+n : n}  return d.getUTCFullYear()+'-'       + pad(d.getUTCMonth()+1)+'-'       + pad(d.getUTCDate())+'T'       + pad(d.getUTCHours())+':'       + pad(d.getUTCMinutes())+':'       + pad(d.getUTCSeconds())+'Z'}  var date = new Date(); var currentISODateTime = ISODateString(date); var ISODateTimeToCompareWith = marker.timestamp;  // Now how do I compare them? 
like image 635
jskidd3 Avatar asked Aug 02 '13 18:08

jskidd3


People also ask

How do I compare two ISO dates?

To compare two dates, you can use either toString() or valueOf() . The toString() method converts the date into an ISO date string, and the valueOf() method converts the date into milliseconds since the epoch.

What is the difference between toISOString and toUTCString?

toISOString() provides millisecond values, whereas . toUTCString() does not.

How do I compare two ISO dates in typescript?

TLDR: To compare dates you can use the toISOString() or getTime() functions that are build into Date . This article just runs through a few examples. Nothing special, we are creating two dates, both the same date, and then we print that to the console so that we can see what the code outputs.

What is the date format according to ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).


1 Answers

Comparing two dates is as simple as

var differenceInMs = dateNewer - dateOlder; 

So, convert the timestamps back into Date instances

var d1 = new Date('2013-08-02T10:09:08Z'), // 10:09 to     d2 = new Date('2013-08-02T10:20:08Z'); // 10:20 is 11 mins 

Get the difference

var diff = d2 - d1; 

Format this as desired

if (diff > 60e3) console.log(     Math.floor(diff / 60e3), 'minutes ago' ); else console.log(     Math.floor(diff / 1e3), 'seconds ago' ); // 11 minutes ago 
like image 194
Paul S. Avatar answered Sep 23 '22 02:09

Paul S.