Reputation: 417
I need to get the difference in seconds between two dates and times.
I have this script:
var date1 = new Date(2013,10,02,12,00,00);
var date2 = new Date(2013,10,02,12,01,00);
var diff = date2 - date1;
diff = diff / 1000;
document.write(diff);
Which returns the value of 60, 60 seconds difference, good.
However, when I cross over a month with 30 days, it doesn't calculate correctly.
var date1 = new Date(2013,9,30,12,00,00);
var date2 = new Date(2013,10,02,12,00,00);
var diff = date2 - date1;
diff = diff / 1000;
document.write(diff);
The result returned is 259200, which is 3 days. The difference between September 30 and October 2 is only 2 days, 172800, because there are only 30 days in the month. Why does Javascript seem to think there are 31 days in September?
Upvotes: 1
Views: 194
Reputation: 413682
The month numbers start with 0, not 1. So 9 is October and 10 is November.
Upvotes: 6