[英]How to return the correct day of the week given a date - year month and day
just to simplify my question i have this following code 为了简化我的问题,我有以下代码
var date = new Date( 2015 , 10 , 16 );
//print the day of the week on console
console.log( date.getDay() ); //prints 1
as far as i understand it should have printed 5 since it is a Friday not Monday but it is not , what could be the problem or am i not comprehending the date object? 据我所知,它应该打印5,因为它是星期五而不是星期一,但它不是,可能是什么问题或者我不理解日期对象?
Thanks. 谢谢。
Your problem is that the month portion of the Date
constructor is 0
-based. 您的问题是
Date
构造函数的月份部分是基于0
的。 (See doc ) (见文件 )
month
月
Integer value representing the month, beginning with 0 for January to 11 for December.
表示月份的整数值,从1月的0开始到12月的11。
You can see this if you were to construct the date in the JavaScript console: 如果要在JavaScript控制台中构造日期,可以看到这个:
new Date( 2015 , 10 , 16 )
> Mon Nov 16 2015 00:00:00 GMT-0500 (EST)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.