我使用這個例子來創建一個有角度的日歷。但我今天意識到,當 DST 結束時,日期會重復,然后從那時起,天數就會減少一天。
看看堆疊閃電戰。11 月 7 日重復。 stackblitz 日歷鏈接
這是生成日歷天的代碼。我如何調整它以便 DST 不會把一切都搞砸?
private getCalendarDays(date = new Date) {
var startDate;
startDate = this.getCalendarStartDay(date).getTime();
const calendarStartTime = startDate;
return this.range(0, 41)
.map(num => new Date(calendarStartTime DAY_MS * num));
}
private getCalendarStartDay(date = new Date) {
const [year, month] = [date.getFullYear(), date.getMonth()];
const firstDayOfMonth = new Date(year, month, 1).getTime();
return this.range(1,7)
.map(num => new Date(firstDayOfMonth - DAY_MS * num))
.find(dt => dt.getDay() === 0)
}
private range(start, end, length = end - start 1) {
return Array.from({ length }, (_, i) => start i)
}
uj5u.com熱心網友回復:
我不太確定,但我想您可以在上午 12 點獲得日期以避免出現問題
private getCalendarStartDay(date = new Date) {
const [year, month] = [date.getFullYear(), date.getMonth()];
//see that I indicate the 1 of the month at 12:00:00
const firstDayOfMonth = new Date(year, month,1,12,0,0,0).getTime();
//well the range from 0 to 6
return this.range(0,6)
.map(num => new Date(firstDayOfMonth - DAY_MS * num))
.find(dt => dt.getDay() === 0)
}
注意:我是在 10 月而不是 11 月收到您的錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/353128.html
標籤:javascript 有角的 日期 日历 天文台
