Posted By

Bluebeard on 11/07/07


Tagged

date difference days work calculate Business


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

korzhik


Calculate Business Days between two dates


 / 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.

  1. function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
  2.  
  3. var iWeeks, iDateDiff, iAdjust = 0;
  4.  
  5. if (dDate2 < dDate1) return -1; // error code if dates transposed
  6.  
  7. var iWeekday1 = dDate1.getDay(); // day of week
  8. var iWeekday2 = dDate2.getDay();
  9.  
  10. iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
  11. iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
  12.  
  13. if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
  14.  
  15. iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
  16. iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
  17.  
  18. // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
  19. iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
  20.  
  21. if (iWeekday1 <= iWeekday2) {
  22. iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
  23. } else {
  24. iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
  25. }
  26.  
  27. iDateDiff -= iAdjust // take into account both days on weekend
  28.  
  29. return (iDateDiff + 1); // add 1 because dates are inclusive
  30.  
  31. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: karantleo on November 21, 2009

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

Posted By: karantleo on November 21, 2009

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.

Posted By: sachinpadha on December 28, 2010

/*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);

date1 = document.getElementById(text1).value;
date2 = document.getElementById(text2).value;


var dt1=date1.split(" ");
//CompareDate1=new Date(dt1[0]);
CompareDate1=dt1[0];
var dttt1 = dt1[1].split(":");
dtt1=dt1[1]+' '+dt1[2];
dttttt1=new Date('12/12/2010'+' '+ dtt1);

if (dttttt1 < startDateTime) {alert('Please enter first date-time equal to or greater than '+StartTime);flag=true;}
if (dttttt1 > EndDateTime) {alert('Please enter first date-time equal to or less than '+EndTime);flag=true;}
var hrs1 = new Date();

var dt2=date2.split(" ");
//CompareDate2=new Date(dt2[0]);
CompareDate2=dt2[0];
var dttt2 = dt2[1].split(":");
dtt2=dt2[1]+' '+dt2[2];
dttttt2=new Date('12/12/2010'+' '+ dtt2);

if (dttttt2 < startDateTime) {alert('Please enter second date-time equal to or greater than '+StartTime);flag=true;}
if (dttttt2 > EndDateTime) {alert('Please enter second date-time equal to or less than '+EndTime);flag=true;}

date1 = new Date(date1)
date2 = new Date(date2)
diff  = new Date();
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

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 (dttt1[0]==12){hoursStart=(parseInt(EndTimeHour)+12)-parseFloat(12+"."+dttt1[1]);}

            if (dt2[2]=='PM'){
                StartTimeHour1=parseFloat(dttt2[0]+"."+dttt2[1])+12;
                hoursEnd= parseInt(StartTimeHour1)-parseInt(StartTimeHour);}
            else{
            hoursEnd= parseFloat(dttt2[0]+"."+dttt2[1])-parseInt(StartTimeHour);}

            if (dttt2[0]==12){hoursEnd=parseFloat(12+"."+dttt2[1])-parseInt(StartTimeHour);}

            hours= parseInt(hoursStart) + parseInt(hoursEnd);

        }
    str2 =hours + " hours ";

} if (mins!=0){ if(mins15 && mins30 && mins45 && mins

Posted By: onlinesolutionproviders on July 28, 2011

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

Posted By: reyncor on November 15, 2014

Fails on dDate1 1416027600000, dDate2 1416546000000

The result should be 6, but it returns 1.

You need to login to post a comment.