Javascript 使用MomentJS获取两个日期之间经过的工作日

Javascript 使用MomentJS获取两个日期之间经过的工作日,javascript,momentjs,Javascript,Momentjs,有没有办法通过MomentJS获得两个日期之间经过的工作日?工作日将是没有周末的一周,或者是周一、周二、周三、周四和周五。到目前为止,我尝试使用diff()方法(),如下所示: var daysElapsed = today.diff(past, 'days'); 据我所知,diff()只提供两个日期之间的所有日期,包括周末。谢谢你的时间 您需要为此编写自己的函数 这并不难,因为你知道确切的开始和结束日期 获取一周中的开始日期和结束日期 仅计算两者之间的周天数 我认为这将是一个非常直接的实现。

有没有办法通过MomentJS获得两个日期之间经过的工作日?工作日将是没有周末的一周,或者是周一、周二、周三、周四和周五。到目前为止,我尝试使用
diff()
方法(),如下所示:

var daysElapsed = today.diff(past, 'days');

据我所知,
diff()
只提供两个日期之间的所有日期,包括周末。谢谢你的时间

您需要为此编写自己的函数

这并不难,因为你知道确切的开始和结束日期

  • 获取一周中的开始日期和结束日期
  • 仅计算两者之间的周天数

  • 我认为这将是一个非常直接的实现。

    您需要为此编写自己的函数

    let start = moment(startDate, 'YYYY-MM-DD'); //Pick any format
    let end = moment(); //right now (or define an end date yourself)
    let weekdayCounter = 0;  
    
    while (start <= end) {
     if (start.format('ddd') !== 'Sat' && start.format('ddd') !== 'Sun'){
       weekdayCounter++; //add 1 to your counter if its not a weekend day
     }
     start = moment(start, 'YYYY-MM-DD').add(1, 'days'); //increment by one day
    }
    console.log(weekdayCounter); //display your total elapsed weekdays in the console!
    
    这并不难,因为你知道确切的开始和结束日期

  • 获取一周中的开始日期和结束日期
  • 仅计算两者之间的周天数
  • 我认为这将是一个非常直接的实现//选择任何格式
    let start = moment(startDate, 'YYYY-MM-DD'); //Pick any format
    let end = moment(); //right now (or define an end date yourself)
    let weekdayCounter = 0;  
    
    while (start <= end) {
     if (start.format('ddd') !== 'Sat' && start.format('ddd') !== 'Sun'){
       weekdayCounter++; //add 1 to your counter if its not a weekend day
     }
     start = moment(start, 'YYYY-MM-DD').add(1, 'days'); //increment by one day
    }
    console.log(weekdayCounter); //display your total elapsed weekdays in the console!
    
    让结束=力矩()//现在(或自己定义结束日期) 让weekdayCounter=0; while(start
    let start=moment(startDate,'YYYY-MM-DD');//选择任何格式
    让end=moment();//现在(或者自己定义一个结束日期)
    让weekdayCounter=0;
    
    尽管这个问题已经很老了,但我还是无意中发现了它,并且找不到真正的答案

    我希望这个解决方案也能对其他人有所帮助:

    const momentStart = moment(initialDate);
    const momentFinal = moment(finalDate);
    
    // Get the diff in days between both dates
    const diff = momentFinal.diff(momentStart, "days");
    
    // If diff is 7 or bigger, all days are included
    if (diff >= 7) {
      return moment.weekdays();
    }
    
    // Need to calculate, check which day we start from
    const firstDay = momentStart.day();
    const weekdaysBetween = [];
    
    // For each diff day, we get the next one)
    for (let i = 0; i <= diff; i++) {
      // use % to loop to beginning again (e.g: start at friday and have +4)
      weekdaysBetween.push(moment.weekdays((firstDay + i) % 7));
    }
    
    return weekdaysBetween;
    
    const momentStart=时刻(初始日期);
    恒动量最终=力矩(最终日期);
    //获取两个日期之间的差值(以天为单位)
    常数差=动量最终差(动量开始,天);
    //如果差异为7或更大,则包括所有天数
    如果(差异>=7){
    返回时刻。工作日();
    }
    //需要计算,检查我们从哪一天开始
    const firstDay=momentStart.day();
    const weekdaysBetween=[];
    //每一天,我们都会得到下一天)
    
    因为(让我=0;我虽然这个问题已经很老了,但我还是无意中发现了它,并且找不到真正的答案

    我希望这个解决方案也能对其他人有所帮助:

    const momentStart = moment(initialDate);
    const momentFinal = moment(finalDate);
    
    // Get the diff in days between both dates
    const diff = momentFinal.diff(momentStart, "days");
    
    // If diff is 7 or bigger, all days are included
    if (diff >= 7) {
      return moment.weekdays();
    }
    
    // Need to calculate, check which day we start from
    const firstDay = momentStart.day();
    const weekdaysBetween = [];
    
    // For each diff day, we get the next one)
    for (let i = 0; i <= diff; i++) {
      // use % to loop to beginning again (e.g: start at friday and have +4)
      weekdaysBetween.push(moment.weekdays((firstDay + i) % 7));
    }
    
    return weekdaysBetween;
    
    const momentStart=时刻(初始日期);
    恒动量最终=力矩(最终日期);
    //获取两个日期之间的差值(以天为单位)
    常数差=动量最终差(动量开始,天);
    //如果差异为7或更大,则包括所有天数
    如果(差异>=7){
    返回时刻。工作日();
    }
    //需要计算,检查我们从哪一天开始
    const firstDay=momentStart.day();
    const weekdaysBetween=[];
    //每一天,我们都会得到下一天)
    
    for(设i=0;谢谢!我已创建以下函数:。谢谢!我已创建以下函数:。