Momentjs 下一个工作日

问题描述 投票:0回答:3

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

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

如何使用 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");
        }
javascript momentjs
3个回答
9
投票

工作正常。您刚刚错过了

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

4
投票

这是另一个版本。这不取决于一周的开始日期:

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

0
投票
if (moment().day() === 5 || moment().day() === 6) { // friday or saturday -> show monday
    $scope.businessDay=moment().day(8).format("MMMM Do YYYY");
} else { // other days, show next day
    $scope.businessDay= moment().add('days', 1).format("MMMM Do YYYY");
}
© www.soinside.com 2019 - 2024. All rights reserved.