我如何调整此功能以使用夏令时?
How can I adjust this function to work with daylight savings?
我用这个例子在 angular 中创建了一个日历。但我今天意识到,当 DST 结束时,日期会重复,然后从那时起日子就会减去一天。
看看 stackblitz。 11 月 7 日重复。 stackblitz link to calendar
这是生成日历日的代码。我该如何调整才能使 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)
}
我不太确定,但我想你可以在 12 a.m 得到日期。避免问题
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 月
我用这个例子在 angular 中创建了一个日历。但我今天意识到,当 DST 结束时,日期会重复,然后从那时起日子就会减去一天。
看看 stackblitz。 11 月 7 日重复。 stackblitz link to calendar
这是生成日历日的代码。我该如何调整才能使 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)
}
我不太确定,但我想你可以在 12 a.m 得到日期。避免问题
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 月