【发布时间】:2020-12-03 10:06:26
【问题描述】:
我正在尝试计算当前和前 6 天的日期。这should work 只需从上一个日期减去 1,所以我实现了我当前的解决方案,如下所示:
let currentDay = new Date();
let nextDay = new Date();
for (let i = 0; i < 7; i++) {
nextDay.setDate(currentDay.getDate() - i);
console.log("i=" + i + "->" + nextDay);
}
但是,这是输出:
i = 0 -> Thu Dec 03 2020 10: 20: 51 GMT + 0100(Central European Standard Time) //today
i = 1 -> Wed Dec 02 2020 10: 20: 51 GMT + 0100(Central European Standard Time) //yesterday
i = 2 -> Tue Dec 01 2020 10: 20: 51 GMT + 0100(Central European Standard Time) //day before yesterday
i = 3 -> Mon Nov 30 2020 10: 20: 51 GMT + 0100(Central European Standard Time) //3 days ago
i = 4 -> Fri Oct 30 2020 10: 20: 51 GMT + 0100(Central European Standard Time) //skips an entire month
i = 5 -> Mon Sep 28 2020 10: 20: 51 GMT + 0200(Central European Summer Time) //skips another month, 2 days and switches to summertime
i = 6 -> Fri Aug 28 2020 10: 20: 51 GMT + 0200(Central European Summer Time) //skips another month
在上个月末运行时,它按预期工作(当前月份没有转义)。它在进入 11 月时失败了。我似乎找不到原因。
【问题讨论】:
标签: typescript date