user431931
user431931

Reputation: 895

JavaScript date comparison oddity

I have an issue with JavaScript date comparison. I create a Date object for 31st Oct, and another for 1st Nov, yet getTime() for each returns the same figure, and the greater-than / less-than operators also consider the dates equal. Here is my code:

d1 = new Date(2012, 10, 31, 0, 0, 0, 0);
d2 = new Date(2012, 11, 1, 0, 0, 0, 0);
document.write(d1.getTime() + "<br />");
document.write(d2.getTime() + "<br />");
document.write((d1 < d2) + "<br />");
document.write((d1 > d2) + "<br />");

And here is the output:

1354320000000
1354320000000
false
false

The same code works fine around other month endings, it seems to be just these two dates that cause an issue.

Any help appreciated!

Upvotes: 1

Views: 102

Answers (1)

Esailija
Esailija

Reputation: 140234

November (10) doesn't have 31 days, so it will wrap to December (11) 1st.

new Date(2012, 10, 31, 0, 0, 0, 0)
//Sat Dec 01 2012 00:00:00 GMT+0200 (FLE Standard Time)

//more wrapping:
new Date(2012, 10, 35, 0, 0, 0, 0)
//Wed Dec 05 2012 00:00:00 GMT+0200 (FLE Standard Time)

Upvotes: 6

Related Questions