Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 日期对象中的2月31天_Javascript_Date - Fatal编程技术网

Javascript 日期对象中的2月31天

Javascript 日期对象中的2月31天,javascript,date,Javascript,Date,此代码应记录给定月份的所有天数: var date = new Date(2012,2,1); var thisMonth = date.getMonth(); while(date.getMonth()==thisMonth) { // 31 steps ??? console.log(date.getMonth(),date.getDate()); date.setDate(date.getDate()+1); } 除了二月,它每个月都能正常工作。有什么想法吗?请注意,月份

此代码应记录给定月份的所有天数:

var date = new Date(2012,2,1);
var thisMonth = date.getMonth();
while(date.getMonth()==thisMonth) { // 31 steps ???
    console.log(date.getMonth(),date.getDate());
    date.setDate(date.getDate()+1);
}

除了二月,它每个月都能正常工作。有什么想法吗?

请注意,月份参数是0-indexed,因此您的代码是关于三月而不是二月

月份

表示月份的整数值,从0开始表示一月到 12月11日。


使用新日期(2012年1月1日)月份以零为基础;-)

这很有趣:

new Date('2014-02-28'); // Fri Feb 28 2014 01:00:00 GMT+0100
new Date('2014-02-29'); // Sat Mar 01 2014 01:00:00 GMT+0100
new Date('2014-02-30'); // Sun Mar 02 2014 01:00:00 GMT+0100
new Date('2014-02-31'); // Mon Mar 03 2014 01:00:00 GMT+0100
new Date('2014-02-32'); // Invalid Date

你在二月份使用什么指数。。。。??1或2…?个月从0开始索引。看,天哪,我好像变老了。。。对于这样一个琐碎的问题,大家都很抱歉。