0

我正在使用这个例子来创建一个角度日历。但我今天意识到,当 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)
  }

4

1 回答 1

0

我不太确定,但我想你可以在上午 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 月

于 2021-11-07T19:53:04.457 回答