2012-10-30 160 views
-1

我知道这个问题是被问了很多的时间,但请帮助我不能够找到解决计数不包括周日和节假日天

与下面的代码中,我能够计算无天的排除平日

现在我想也排除节假日说,对如2012年8月15日,2012年9月12日,2012年12月20日,请大家帮忙

function namet() 
{ 

     var iWeeks, iDateDiff, iAdjust = 0; 

     var nodays = document.getElementById("timestamp1").value; 


     var nodays1 = document.getElementById("timestamp").value; 



     var dDate1 = new Date(nodays1); 



     var dDate2 = new Date(nodays); 





     if (dDate2 < dDate1) { 

      alert("End Date : Enter date more than Start Date "); 
     } 
     // error code if dates transposed 

     var iWeekday1 = dDate1.getDay(); // day of week 



     var iWeekday2 = dDate2.getDay(); 
     iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7 
     iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2; 
     if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend 
     iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays 
     iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; 

     // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000) 
     iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime())/604800000) 

     if (iWeekday1 <= iWeekday2) { 
      iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1) 
     } else { 
      iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2) 
     } 

     iDateDiff -= iAdjust // take into account both days on weekend 

     var final = (iDateDiff + 1); // add 1 because dates are inclusive 

     document.leaveapplication.noofdays3.value = final; 
+1

什么是公共假期。你需要指定。它随时间变化,国家到国家 – polin

+0

我假设你的意思是'不包括星期*结束*和假期',因为假期一般都是平日。 –

+0

是说例如公众假期是:2012年8月15日,2012年9月20日,2012年12月20日,也排除星期六是星期六和星期日 – jim

回答

0

你需要假期的列表这是平日里的事情。

  1. 起点和终点之间的计算天
  2. 起点和终点之间
  3. 计算周末(天模7,然后调整在最后一周的位置)
  4. 计算所有的假期的日期之间,您的列表也减去这一点。
+1

亲爱的菲尔,如果可能请你修改代码将排除平日和节假日,请发送完整的代码,请帮助 – jim

相关问题