我有一個查詢,我需要計算從今天到給定數字的作業天數,
比如今天是09/14/2021,給定數字是10,我希望最后的日期是09/28/2021,因為從今天開始是第10個作業日。
比如今天是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 ) =>/span> {
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) 。
}
在計算中加入這些天數后,我得到了另一個日期,但我很困惑,如果在新的日期范圍內又有周末,那么我可以對這些天數做什么。
所以誰能指導我一下呢?
uj5u.com熱心網友回復:
試試這個:
。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
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
另一種解決方案
startDate = new Date("09/17/2021">) 。
endDate = new Date("10/28/2021")。
//計算 days_difference = (endDate milliseconds - startDate milliseconds) / (1000 * 60 * 60 * 24) 以獲得以天為單位的結果
var days_difference = (endDate.getTime() - startDate.getTime() / 86400000。
//計算整個期限內的周末數。
var weekdends = Math.floor(day_difference/7) * 2 。
//調整周末取決于開始的日子。
(day_difference%7) startDate.getDay() == 6 ? weekdends = 2 :
(day_difference%7) startDate.getDay() == 5 ? weekdends =1 :
weekdends;
//計算作業日
var workDays = days_difference - weekdends
console.log("Working days",(workDays));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/310886.html
標籤:
