I need to write JavaScript that allows me to compare two ISO timestamps and then print the difference between them, for example: "32 seconds".
Below is the function I found in Stack Overflow, it turns a regular date into a formatted ISO. So, the first thing to do is get the current time in ISO format.
The next thing I need to do is get another ISO timestamp to compare it, well, I have what is stored in the object. It can be accessed as follows: marker.timestamp (as shown in the code below). Now I need to compare these two timestamps and work out the difference between them. If it is & lt; 60 seconds, it should be displayed in seconds, if it is> 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;
source share