简体   繁体   中英

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?

Thanks.

Your problem is that the month portion of the Date constructor is 0 -based. (See doc )

month

Integer value representing the month, beginning with 0 for January to 11 for December.

You can see this if you were to construct the date in the JavaScript console:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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