开发者

javascript date.utc problem

开发者 https://www.devze.com 2023-01-01 13:41 出处:网络
I\'m trying to compare 2 dates using javascript. 1 at the end of the month and 1 at the beginning. I need to compare these 2 dates in seconds so I\'m using the Date.UTC javascript function.

I'm trying to compare 2 dates using javascript. 1 at the end of the month and 1 at the beginning. I need to compare these 2 dates in seconds so I'm using the Date.UTC javascript function.

Here's the code:

var d = Date.UTC(2010,5,31,23,59,59);
document.write(d);

var开发者_开发百科 d2 = Date.UTC(2010,6,1,12,20,11);
document.write(d2);

The output for is:

1278028799000
1277986811000

This is telling me that 1/6/2010 is less than 5/31/2010 in milliseconds.

How is that possible? What am I doing wrong?

Thanks for your help.


The month parameter to Date.UTC() is 0-indexed; January is 0, February is 1, etc.

UTC() will try to adapt invalid dates, so it's converting "June 31, 2010" into "July 1, 2010". Then, the extra 23:59:59 is making the first date larger.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号