javascript - 如何使用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);
}
并且在计数中添加这些天数给了我另一个日期,但是如果在新的日期范围内还有周末,那么我可以用那几天做什么。
所以任何人都可以请指导我。
解决方案
试试这个 :
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
推荐阅读
- stripe-payments - Stripe:更改计划,不要按比例分配未使用的时间
- python - 在 dask.Array 任务图中嵌入计算前/计算后操作
- javascript - 在页面上获取视频的位置 – JS
- mysql - mysql中的IF函数
- javascript - 道具'coordinate.longitude'在'MapMarker'中被标记为必需,但它的值是未定义的
- python - Groupby & Sum - 使用添加的 If 条件创建新列
- c# - CreateInstance 在运行时使用管道失败
- c++ - 潜在的内存泄漏,获取子向量
- function - 如何在控制器Codeginiter 4中调用同一类中的函数
- ruby - 在 MAC 上安装 ruby 3.0.2 时出错:运行时出错 '__rvm_make -j8' 错误