【问题标题】:Momentjs Next Business DayMomentjs 下一个工作日
【发布时间】:2015-07-17 13:20:56
【问题描述】:

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

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

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

【问题讨论】:

  • 你需要一个包含所有假期的数组,如果还有的话也要检查

标签: javascript momentjs


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2012-06-12
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多