javascript date.utc问题 [英] javascript date.utc problem

查看:116
本文介绍了javascript date.utc问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript比较2个日期。 1个月底,1个在开始。我需要在几秒钟内比较这两个日期,所以我使用Date.UTC javascript函数。



这里是代码:

 
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);

输出为:

 
1278028799000
1277986811000

这告诉我, 1/6/2010 小于 5/31/2010 以毫秒为单位。



怎么可能?我做错了什么?



感谢您的帮助。

解决方案

month 参数到 Date.UTC()是0索引; 1月是0,2月是1等。



UTC()将尝试修改无效日期,将2010年6月31日转为2010年7月1日。然后,额外的23:59:59使第一个日期变大。


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.

这篇关于javascript date.utc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆