Momentjs 下一工作日

Momentjs Next Business Day

我正尝试在下一个工作日使用我的代码,但它无法正常工作。它只是在第二天显示。我试图检查其他问题以找出错误,但我仍然无法弄清楚。

这个问题适用于我自己但不完全是:

How to exclude weekends between two dates using Moment.js

 $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");
        }

一切正常。您刚刚错过了 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");
    }

这是另一个版本。这与星期几无关:

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);
}