Reputation: 15430
I just encountered two dates in JavaScript difference between date and ISO date. Can someone put some light on what is the difference between these two?
Upvotes: 2
Views: 8717
Reputation: 207501
The MDN documentation has a good amount of information on the Date Object.
They have an example on how to produce iso-8601 formatted dates.
Upvotes: 5
Reputation: 11
i found a online calculator for difference between two date timestamps http://www.onlineconversion.com/days_between_advanced.htm its their source code
var ts1='2020-02-10T08:07:29.058Z'
var ts2='2020-02-10T10:56:32.021Z'
function p (i)
{
return Math.floor(i / 10) + "" + i % 10;
}
function trunc (i)
{
var j = Math.round(i * 100);
return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}
function calculate (ts1,ts2)
{
var date1 = new Date(ts1);
console.log(date1)
var date2 = new Date(ts2);
console.log(date2)
var sec = date2.getTime() - date1.getTime();
if (isNaN(sec))
{
console.log("Input data is incorrect!");
return;
}
if (sec < 0)
{
console.log("The second date ocurred earlier than the first one!");
return;
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
var inHour = trunc(sec / hour);
var inMin = trunc(sec / minute);
var inSec = trunc(sec / second);
var days = Math.floor(sec / day);
sec -= days * day;
var hours = Math.floor(sec / hour);
sec -= hours * hour;
var minutes = Math.floor(sec / minute);
sec -= minutes * minute;
var seconds = Math.floor(sec / second);
var inDays = days + " day" + (days != 1 ? "s" : "") + ", " + hours + " hour" + (hours
!= 1 ? "s" : "") + ", " + minutes + " minute" + (minutes != 1 ? "s" : "") + ", " +
seconds + " second" + (seconds != 1 ? "s" : "");
var duration =hours + " hour" + (hours != 1 ? "s" : "") + ", " + minutes + " minute" +
(minutes != 1 ? "s" : "") + ", " + seconds + " second" + (seconds != 1 ? "s" : "");
console.log('in days ',inDays)
console.log('in days ',duration)
console.log('in hours ',inHour)
console.log('in mins ',inMin)
console.log('in secs ',inSec)
}
calculate(ts1,ts2)
Upvotes: 1
Reputation: 1587
ISO stands for International Organization for Standardization. The ISO standard organizes the data so the largest temporal term (the year) appears first in the data string and progresses to the smallest term. EX: 2011-07-14.
Javacript's getDate() function from the date object returns the day of the month(1-31). Javascript also has a few other functions you might want to use, such as:
var d = new Date();
d.getDay()
d.getFullYear()
d.getHours()
d.getMinutes()
d.getSeconds()
Good documentation is available here: http://www.w3schools.com/jsref/jsref_obj_date.asp
To sum it all up, ISO date is just a commonly used format for displaying the date.
Upvotes: 8