首页 > 解决方案 > 如何使用js计算排除周末后的天数

问题描述

我有一个查询,我需要计算从今天到给定数字的工作日数,

就像今天是 09/14/2021 并且给定的数字是 10 我想要最后一个日期 09/28/2021,因为它是从今天起的第 10 个工作日。我试着在下面计算不同的日子

    var startDate = new Date();
    var endDate = new Date();
    if(res.defaultLeadTime !== 0){
        endDate.setDate(startDate.getDate() + 10);
    }

我是 2021 年 9 月 24 日。现在我正在计算两个日期之间的周末

countWeekendDays = ( d0, d1 ) => {
    var ndays = 1 + Math.round((d1.getTime()-d0.getTime())/(24*3600*1000));
    var nsaturdays = Math.floor( (d0.getDay()+ndays) / 7 );
    return 2*nsaturdays + (d0.getDay()==0) - (d1.getDay()==6);
}

并且在计数中添加这些天数给了我另一个日期,但是如果在新的日期范围内还有周末,那么我可以用那几天做什么。

所以任何人都可以请指导我。

标签: javascriptjquerydate

解决方案


试试这个 :

var startDate = "14-SEPT-2021";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 10, count = 0;
while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       count++;
    }
}
alert(endDate);//You can format this date as per your requirement


推荐阅读