简体   繁体   English

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

[英]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.

相关问题 如何获得星期几和一年中的月份? - How to get the day of week and the month of the year? 返回未来日期 - Javascript(年,月,日) - Return future date - Javascript (year, month, day) Javascript、时间和日期:获取给定毫秒时间的当前分钟、小时、日、周、月、年 - Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond time 对“日”“周”“月”和“年”进行排序 - Sorting "day" "week" "month" and "year" 如何计算仅给定月份、该月内的周数和星期几的日期 - How to calculate a Date given only month, week number within that month, and day of week Javascript,时间和日期:获取下一天,周,月,年等 - Javascript, Time and Date: Getting the next day, week, month, year, etc 在javascript中给出一年,一周和一天的第一个星期日期 - Getting first date in week given a year,weeknumber and day number in javascript 如何在javascript中按小时/6小时/天/周/月/年对日期对象进行分组 - How to group date object by hour/6 hours /day/week/month/year in javascript JS datepicker能够:时间,日期,日期时间,星期几,一年中的一周,一年中的一个月等 - JS datepicker capable of: time, date, datetime, day of week, week of year, month of year etc JavaScript:获取给定日期(月/年)的日期名称数组 - JavaScript: get array of day names of given date (month/year)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM