gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:38:25 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com