只是为了简化我的问题,我有以下代码
var date = new Date( 2015 , 10 , 16 );
//print the day of the week on console
console.log( date.getDay() ); //prints 1
据我所知它应该打印 5 因为它是星期五而不是星期一但它不是,可能是什么问题或者我不理解日期对象?
谢谢。
最佳答案
您的问题是 Date
构造函数的月份部分是基于 0
的。 (参见 doc)
month
Integer value representing the month, beginning with 0 for January to 11 for December.
如果您要在 JavaScript 控制台中构建日期,您可以看到:
new Date( 2015 , 10 , 16 )
> Mon Nov 16 2015 00:00:00 GMT-0500 (EST)
关于javascript - 如何在给定日期的情况下返回正确的星期几 - 年月日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33136387/