问题描述
我正在尝试使用我的代码获得下一个工作日,但无法正常工作。 仅在第二天显示。 我尝试检查其他问题以查找错误,但仍然无法弄清楚。
这个问题适用于我自己的问题,但不完全适用:
$scope.businessDay = new Date();
if (moment().day() === 5) { // friday, show monday
// set to monday
$scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
}
if (moment().day() === 6) { // saturday, show monday
// set to monday
$scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
}
else { // other days, show next day
$scope.businessDay= moment().add('days', 1).format("MMMM Do YYYY");
}
1楼
一切正常。
你刚刚错过了else
if (moment().day() === 5) { // friday, show monday
// set to monday
$scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
--> } else if (moment().day() === 6) { // saturday, show monday
// set to monday
$scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
}
else { // other days, show next day
$scope.businessDay= moment().add('days', 1).format("MMMM Do YYYY");
}
2楼
这是另一个版本。 这不取决于星期几:
function buildNextValidDate(dateFormat = 'MMMM Do YYYY') {
let dayIncrement = 1;
if (moment().day() === 5) {
// set to monday
dayIncrement = 3;
} else if (moment().day() === 6) {
// set to monday
dayIncrement = 2;
}
return moment().add(dayIncrement, 'd').format(dateFormat);
}