繁体   English   中英

如何在给定日期 - 月份和日期的情况下返回正确的星期几

[英]How to return the correct day of the week given a date - year month and day

为了简化我的问题,我有以下代码

var date = new Date( 2015 , 10 , 16 );

//print the day of the week on console
console.log( date.getDay() ); //prints 1

据我所知,它应该打印5,因为它是星期五而不是星期一,但它不是,可能是什么问题或者我不理解日期对象?

谢谢。

您的问题是Date构造函数的月份部分是基于0的。 (见文件

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

如果要在JavaScript控制台中构造日期,可以看到这个:

new Date( 2015 , 10 , 16 )
> Mon Nov 16 2015 00:00:00 GMT-0500 (EST)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM