Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.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 检查月日和年日,结果不正确_Javascript - Fatal编程技术网

Javascript 检查月日和年日,结果不正确

Javascript 检查月日和年日,结果不正确,javascript,Javascript,我正在使用下面的代码检查一个月的哪一天,但它不工作。请帮我做这个 function daysInMonth(month, year) { var length = new Date(year, month, 0).getDate(); for(var i = 1; i < = length; i++) { console.log(new Date(year,month,i).getDay()); } }; 功能日月(月,

我正在使用下面的代码检查一个月的哪一天,但它不工作。请帮我做这个

  function daysInMonth(month, year) {
     var length =  new Date(year, month, 0).getDate();
     for(var i = 1; i < = length; i++)
     {
         console.log(new Date(year,month,i).getDay());
     }
  };
功能日月(月,年){
变量长度=新日期(年、月、0)。getDate();
对于(变量i=1;i<=长度;i++)
{
log(新日期(年、月、i).getDay());
}
};
这是小提琴。它返回的结果不正确。

构造函数将param month作为
0
11
之间的整数,因此您的,使用
daysInMonth(122012);​
实际上是在
daysInMonth(02013)中查找一周中的几天;​,即2013年1月而非2012年12月

下面是一些代码,可以让您使用月份作为
1
12

function daysInMonth(month, year) {
    var i, length =  new Date(year, month, 0).getDate(); // get last day no. of previous month ( month 0 - 11 )
    month = month - 1; // set month int to what we want
    for (i = 1; i <= length; i++) {
        console.log(new Date(year,month,i).getDay()); // continue as before
    }
};

daysInMonth(12,2012);
功能日月(月,年){
变量i,长度=新日期(年,月,0).getDate();//获取上个月的最后一天编号(月0-11)
month=month-1;//将month int设置为我们想要的
对于(i=1;i而言,这是有效的:

console.log(new Date('12/15/2012').getDay());​
Paul S.是对的,工作原理如下:

console.log(new Date(2012, 11, 15));​
显示月份从0开始,在您的示例中:

daysInMonth(11,2012);​

以下是正确的代码:

function daysInMonth( month, year ) {
    var day;
    for( var i = 1 ; i <= new Date( year, month, 0 ).getDate() ; i++ ) {
        day = new Date( year, month-1, i );
        console.log( day, day.getDay() );
    }

};

daysInMonth( 12, 2012 );

可供选择的较短代码:

function daysInMonth( month, year ) {
    for( var i = new Date( year, month, 0 ).getDate(), d = new Date( year, month-1, 1 ).getDay() ; i-- ; )
        console.log( d++ % 7 );
};

为什么你认为它不正确?
.getDay()
是一周中的一天。使用
.getDate()
或只使用
i
(月中的一天)同样在循环中。我用2012年12月对它进行了测试。根据日历,开始和结束索引日期是错误的。众所周知,日期索引从0开始。@PaulS。日志记录我返回日期计数。但我只是想知道我在那一年的哪一天和monthOh,然后你解决了自己的问题;索引从0开始适用于也有个月(您的演示目前是2013年1月)。没有Paul S,它给出了确切的结果。这里的问题是它需要从1到12个月的计数。尝试通过2月和2003年。您将得到确切的结果。根据您的评论,它应该返回29。但它返回28。@A.V
Date(year,month,0)
=>0小于1,减去1天,=>前一天的最后一天month@SoI使用小提琴上的代码,如果您试图以
2
的形式通过Feb,并使用
2003
,您将得到2003年2月的天数(即28天)然后你会做2003年1月1日到28日的工作。@PaulS。谢谢你的评论。在我的示例中,月份从1开始。因此12个月是有效的。月份从0到11,你写的是12个月,所以这是不正确的工作,正如预期的那样。谢谢。我可以知道如果我们超过0000年、000120002年或9999年,它会工作吗?日期应该可以达到100000,001970年1月1日UTC任意一侧0天,没有问题,即公元前271702年至公元前275642年。0的前缀可能会产生意外行为,尽管形式为
0+[0-7]+
的文字表示您是以八进制而非十进制书写。
function daysInMonth( month, year ) {
    for( var i = new Date( year, month, 0 ).getDate(), d = new Date( year, month-1, 1 ).getDay() ; i-- ; )
        console.log( d++ % 7 );
};