Javascript中两个日期的初始化差异 [英] Difference in initialization of two dates in Javascript

查看:110
本文介绍了Javascript中两个日期的初始化差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这些两个日期不同

  var date1 = new Date(); 
date1.setFullYear(2012); //年(四位数)
date1.setMonth(10); //月(从0-11)
date1.setDate(1); //每月的一天(从1-31起)

var date2 = new Date(2012,10,1,0,0,0,0);

结果:

 code>日期1:星期六2012年12月01日14:56:16 GMT + 0100 
日期2:星期四01 01 01 00:00:00 GMT + 0100

而这些两个日期相等

  var date3 = new Date(); 
date3.setFullYear(2012); //年(四位数)
date3.setMonth(9); //月(从0-11)
date3.setDate(1); //月的日期(从1-31)

var date4 = new Date(2012,9,1,0,0,0,0);

结果:

 code>日期3:2012年10月01日14:56:16 GMT + 0200 
日期4:2012年10月01日00:00:00 GMT + 0200
另一个问题是为什么 date1.setMonth(10)在十二月提供一个日期(应该是11月)。

解决方案

最后得到了。 new Date()将日期设置为当前日期和时间。换句话说,10月31日(在撰写本文时)。



当您尝试将月份设置为11月时,该怎么办? 11月只有30天...所以它包装到十二月。



如果您更改订单,以便您设置日期之前的之前这个月,它有效:

  var date1 = new Date(); 
date1.setFullYear(2012); //年(四位数)
date1.setDate(1); //月的日子(从1-31起)
date1.setMonth(10); //月(从0-11)

或者由jbabey的答案暗示:

  var date1 = new Date(); 
date1.setFullYear(2012); //年(四位数)
date1.setMonth(10,1); //月(从0-11)和日(1-31)

文档并不是很明显,但至少是提示:


如果您指定的参数超出预期范围,setMonth会相应地更新Date对象中的日期信息。例如,如果您使用15个月份值,则年份将增加1(年+ 1),3个将用于月份。


(因此是不精确的,但这意味着实施至少可以说是正确的...)


Why do these two dates are differents :

var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setMonth(10); // month (from 0-11)
date1.setDate(1); // day of the month (from 1-31)

var date2 = new Date(2012, 10, 1, 0, 0, 0, 0);

Result :

Date 1 : Sat Dec 01 2012 14:56:16 GMT+0100
Date 2 : Thu Nov 01 2012 00:00:00 GMT+0100

whereas these two dates are equals :

var date3 = new Date();
date3.setFullYear(2012); // year (four digits)
date3.setMonth(9); // month (from 0-11)
date3.setDate(1); // day of the month (from 1-31)

var date4 = new Date(2012, 9, 1, 0, 0, 0, 0);

Result :

Date 3 : Mon Oct 01 2012 14:56:16 GMT+0200
Date 4 : Mon Oct 01 2012 00:00:00 GMT+0200

Another question is why do date1.setMonth(10) gives a date in December (should be November).

解决方案

Finally got it. new Date() sets the date to the current date and time. In other words, October 31st (at the time of this writing).

When you then try to set the month to November, what's it to do? November only has 30 days... so it wraps it round to December.

If you change the order so that you set the day-of-month before the month, it works:

var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setDate(1); // day of the month (from 1-31)
date1.setMonth(10); // month (from 0-11)

Or as implied by jbabey's answer:

var date1 = new Date();
date1.setFullYear(2012); // year (four digits)
date1.setMonth(10, 1); // month (from 0-11) and day (1-31)

The documentation isn't terribly clear, but it's at least suggestive:

If a parameter you specify is outside of the expected range, setMonth attempts to update the date information in the Date object accordingly. For example, if you use 15 for monthValue, the year will be incremented by 1 (year + 1), and 3 will be used for month.

("Accordingly" is far from precise, but it means the implementation is at least arguably correct...)

这篇关于Javascript中两个日期的初始化差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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