我正在建立一個使用JavaScript(Vue JS)的日曆。我有這個小方法來幫助獲得當月的日子:JS日曆短兩天
daysInYearMonth(y,m){
return new Date(y, m, 0).getDate()
}
如果我console.log上面我得到31,因爲它是七月。但是,當它使用這個值時:
this.days = [...Array(this.daysInYearMonth(this.year,
this.month)).keys()]
}
我console.log this.days並得到29天,兩個短。如果我只是嘗試添加兩個,那麼其他幾個月就關閉了。這是我的CodePen。
注意:我故意不使用Moment.js,儘管我同意它更好,但團隊決定只使用Vue和vanilla JS。
在此處發佈您的代碼,而不僅僅是在遠程站點。您可以使用[Stack Snippets](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/)使其可執行。 – Barmar
@Barmar我相信這是所有相關的代碼。但是我確實看到了讓代碼在問題中可執行的價值,所以如果鏈接變壞,它仍然有價值。我會創建一個片段,我不知道這個功能,謝謝! – Auzy
'new Date()。getMonth()'今天是6.並且Array(x)將基於零。 – Bert