繁体   English   中英

如何使用moment js让第二天跳过周末

[英]How to get the next day skipping the weekend using moment js

我正在使用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.');
  }

您可以使用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;
}

您可以使用moment().hour()检查 hours 和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>

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

weekday 将返回一个介于 0 到 6 之间的值,其中 0 是星期日,6 是星期六,请在此处阅读更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM