二月31日在Date对象中 [英] 31 days in February in Date object

查看:105
本文介绍了二月31日在Date对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码应记录给定月份的所有日期:

This code should log all days for given month:

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

它适用于每个月或二月。任何想法在哪里?

It works well for every month but February. Any ideas where is the catch?

推荐答案

请注意,月份参数是 0索引,所以你的代码是关于三月而不是二月

Note the month parameter is 0-indexed, so your code is about March not February.

文档:


代表月份的整数值,以 0开头1月至
11为12月。

Integer value representing the month, beginning with 0 for January to 11 for December.

这篇关于二月31日在Date对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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