我是JS的新手,如果这是常识,请对不起。我尝试搜索,但找不到我想要的东西。
我创建了一个计划系统,其一部分工作是计算完成日期。它已经工作了几个月,但是我的一个用户注意到某些时间表是提早一天。修复了一些错误之后,我将问题缩小为一行代码和一个特定的日期。日期是3月31日(星期日)(英国夏令时更改为夏令时)。
function calculateCompletionDate(startDate, duration) {
let currentDate = new Date(startDate);
while (duration > 0) {
let check = checkIsNotWeekend(currentDate);
if (check) {
if (duration === 1) {
break;
}
currentDate.setDate(currentDate.getDate() + 1);
duration--;
}
if (!check && duration >= 1) {
currentDate.setDate(currentDate.getDate() + 1); // this line fails on March 31st!!
}
}
return currentDate;
}
我发现的问题是在3月31日,增量仅增加了23小时2019-03-31T00:00:00.000Z-2019-03-31T23:00:00.000Z影响力是一天的损失。
有人知道我在做什么错吗?
非常感谢
丹