计算两天之间的工作日应排除假期

我想计算两天之间的工作日,不包括假期[数组]。 此代码有助于计算工作日,但不排除假期:

 function calcBusinessDays(dDate1, dDate2) { // input given as Date objects var iWeeks, iDateDiff, iAdjust = 0; if (dDate2  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 return (iDateDiff + 1); // add 1 because dates are inclusive }  

假期列表:

 natDays = [ [1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'], [7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'], [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke'] ]; function nationalDays(date) { for (i = 0; i < natDays.length; i++) { if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) { return [false, natDays[i][2] + '_day']; } } return [true, '']; } 

date.js是一个非常方便的日期库,具有各种便利方法,如between() ,可以大大简化您的问题。

http://code.google.com/p/datejs/wiki/APIDocumentation#between