只是爲了簡化我有這下面的代碼如何返回給定日期的一週正確的日期 - 年月和
var date = new Date(2015 , 10 , 16);
//print the day of the week on console
console.log(date.getDay()); //prints 1
,據我瞭解應該印5,因爲它是我的問題星期五不是星期一,但不是,可能是什麼問題,或者我不理解日期對象?
謝謝。
只是爲了簡化我有這下面的代碼如何返回給定日期的一週正確的日期 - 年月和
var date = new Date(2015 , 10 , 16);
//print the day of the week on console
console.log(date.getDay()); //prints 1
,據我瞭解應該印5,因爲它是我的問題星期五不是星期一,但不是,可能是什麼問題,或者我不理解日期對象?
謝謝。
您的問題是Date
構造函數的月份部分是0
-based。 (見doc)
月
整數值表示的月份,以0開頭的1月至11月進行。
你可以看到這一點,如果你是構建在JavaScript控制檯日期:
new Date(2015 , 10 , 16)
> Mon Nov 16 2015 00:00:00 GMT-0500 (EST)
也getDay()根據美國日曆(星期日是第一天)返回從0到6的值。鏈接[這裏](http://www.w3schools.com/jsref/jsref_getday.asp) –
更明確,這意味着你不看在10月16日但11月16日(這的確是一個星期一) – ochi
@ kikyalex,好點,雖然OP確實提到它應該在週五打印5,這是正確的。但是,是的,星期天將是'0',而不是'7'。 – arcyqwerty
月是'0'基礎的 – arcyqwerty
可能的複製[一週的日不正確輸入日期字段顯示( http://stackoverflow.com/questions/17448651/incorrect-day-of-week-showing-in-input-date-field) –
嘗試'console.log(日期);'就在你的'console.log(日期.getDay());' – Anonymous0day