/ Published in: JavaScript
This routine is loosely based on elightbo's "Calculate Business Days" snippet in ASP, with some process & logic modifications (most notably accounting for situations when both dates fall on a weekend) and a translation to JavaScript.
Expand |
Embed | Plain Text
function calcBusinessDays(dDate1, dDate2) { // input given as Date objects var iWeeks, iDateDiff, iAdjust = 0; if (dDate2 < dDate1) return -1; // 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 return (iDateDiff + 1); // add 1 because dates are inclusive }
Comments
Subscribe to comments
You need to login to post a comment.

Don't know much about JS but logically line 21 comparison is flawed because it compares adjusted weekdays (which were adjusted on lines 15 and 16) as opposed to original weekdays. For example, if date1 is Saturday last week and date2 is Friday this week, this routine will yield 0 days, which is wrong.
One way to deal with this is to capture original weekdays before adjusting them on lines 15 and 16 like so:
iOriginalWeekday1 = iWeekday1 iOriginalWeekday2 = iWeekday2
And then use these variables on line 21 instead:
if (iOriginalWeekday1
Hmm, the last line of comment was modified for some reason. In any case, you need to check if iOriginalWeekday1 is less than or equal to iOriginalWeekday2.
/*In this function the start time is 10:00 AM and End time is 07:00 PM.
For the holidays you need to play with number of days. That thing is still remaining. If you can do it yourself by subtracting the number of days according to your holidays. If you wouldn't able to do so then I am here for you :)*/
function calfx(text1,text2,text3) { var second=1000, minute=second60, hour=minute60, day=hour24, week=day7; var StartTime='10:00 AM'; var EndTime='07:00 PM'; var StartTimeHour='10'; var EndTimeHour='7'; var flag=false; var startDateTime = new Date('12/12/2010'+' '+ StartTime); var EndDateTime = new Date('12/12/2010'+' '+ EndTime);
var str=""; var str1=""; var str2=""; var str3=""; var str4=""; if (weeks!=0){ str =weeks + " weeks "; } if (days!=0){ str1 =days + " days "; } if (hours!=0){ if (CompareDate1 != CompareDate2){ var hoursStart = 00; var hoursend = 00; if (dt1[2]=='PM'){ hoursStart= parseInt(EndTimeHour)-parseFloat(dttt1[0]+"."+dttt1[1]);} else{ EndTimeHour1=parseInt(EndTimeHour)+12; hoursStart= parseInt(EndTimeHour1)-parseFloat(dttt1[0]+"."+dttt1[1]);}
} if (mins!=0){ if(mins15 && mins30 && mins45 && mins
This is nice post and sprituly encourges others becuase under standing value excellnt last when web site i read post i real inspired and suddenly bookmar many friends share me information. onlinesolutionproviders http://www.onlinesolutionproviders.com