【问题标题】:How to get the next day skipping the weekend using moment js如何使用moment js让第二天跳过周末
【发布时间】:2020-03-16 00:01:45
【问题描述】:

我正在使用 moment.js 处理日期。

上午 10 点之前下的订单将在当天处理,上午 10 点之后的订单将在第二天处理。但是当有人在星期五上午 11 点下订单时,我不希望下一个处理日是星期六,而是下周星期一。

如何使用 moment.js 做到这一点?

目前这是我的代码(还没有检查上午 10 点,我稍后会添加):

  // Set locale to dutch
  moment.locale('nl');

  // Set current day and set full date to use in string later on
  $dag = moment().format('dddd');
  $volledatum = $dag +' '+ moment().format('D MMMM');

  // Check if day is saturday or sunday
  if($dag == 'zaterdag' || $dag == 'zondag'){
    // It's the weekend so $volledatum should be the first day after the weekend
    $('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>('+ $volledatum +') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
  }else{
    // It's not the weekend so use the current date
    $('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>('+ $volledatum +') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
  }

【问题讨论】:

    标签: javascript jquery date momentjs


    【解决方案1】:

    您可以使用moment().hour() 来检查小时,moment.day() 可以返回星期几,星期日为 0,星期六为 6:

    // Set locale to dutch
    moment.locale('nl');
    
    // Set current day and set full date to use in string later on
    const dag = moment('2019-11-23T11:01:00');
    const volledatum = dag.format('dddd D MMMM');
    // Check if day is saturday or sunday
    let isWeekend = (dag.day() === 6 || dag.day() === 0) && dag.hour() >= 11;
    console.log('isWeekend', isWeekend);
    if (isWeekend) {
      // It's the weekend so $volledatum should be the first day after the weekend
      $('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>(' + volledatum + ') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
    } else {
      // It's not the weekend so use the current date
      $('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>(' + volledatum + ') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/nl.js"></script>
    <div id="datumcheck"></div>

    【讨论】:

      【解决方案2】:

      使用moment.weekday() 获取当前星期几,使用moment.hours() 获取当前时间小时数。

      weekday 将返回一个介于 0 到 6 之间的值,其中 0 是星期日,6 是星期六,阅读更多 here

      【讨论】:

        【解决方案3】:

        你可以使用moment-business-days

        例子:

        var momentBusinessDays = require("moment-business-days")
        
        momentBusinessDays('20-09-2018', 'DD-MM-YYYY').businessAdd(1)
        

        如果你不想使用另一个外部库,你可以使用类似的东西:

        function addBusinessDays(originalDate, numDaysToAdd) {
          const Sunday = 0;
          const Saturday = 6;
          let daysRemaining = numDaysToAdd;
        
          const newDate = originalDate.clone();
        
          while (daysRemaining > 0) {
            newDate.add(1, 'days');
            if (newDate.day() !== Sunday && newDate.day() !== Saturday) {
              daysRemaining--;
            }
          }
        
          return newDate;
        }
        

        【讨论】:

        • 谢谢,我已通过工作日延期解决了这个问题。
        猜你喜欢
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 1970-01-01
        相关资源
        最近更新 更多