Find day difference between two dates (excluding weekend days)

Viewed 56449

Hi i am using jquery-ui datepicker to select date and date.js to find difference between 2 dates.

Right now the problem is I want to exclude weekend days from calculation (saturday and sunday). How should i do that?

For example the user select start date (13/8/2010) and end date (16/8/2010). Since 14/8/2010 and 15/8/2010 is in week days, instead of 4 days total, i want it to be only 2 days.

This is the code im using right now:

<script type="text/javascript">

    $("#startdate, #enddate").change(function() {       

    var d1 = $("#startdate").val();
    var d2 = $("#enddate").val();

            var minutes = 1000*60;
            var hours = minutes*60;
            var day = hours*24;

            var startdate1 = getDateFromFormat(d1, "d-m-y");
            var enddate1 = getDateFromFormat(d2, "d-m-y");

            var days = 1 + Math.round((enddate1 - startdate1)/day);             

    if(days>0)
    { $("#noofdays").val(days);}
    else
    { $("#noofdays").val(0);}


    });

    </script>
13 Answers

Important: Most answers here don't actually work if the start date (or sometimes the end date) is a saturday or sunday. I took the accepted response and modified it so that this issue is resolved now:

    var dateDiff;
    if (dateTo < dateFrom) return -1; // error code if dates transposed
    var dateFromDayOrig = dateFrom.getDay(); // day of week
    var dateToDayOrig = dateTo.getDay();
    var dateFromDay = (dateFromDayOrig == 0) ? 7 : dateFromDayOrig; // change Sunday from 0 to 7
    var dateToDay = (dateToDayOrig == 0) ? 7 : dateToDayOrig;
    dateFromDay = (dateFromDay > 5) ? 5 : dateFromDay; // only count weekdays
    dateToDay = (dateToDay > 5) ? 5 : dateToDay;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
    var weekDifference = Math.floor((dateTo.getTime() - dateFrom.getTime()) / 604800000);

    if (dateFromDay <= dateToDay) {
        dateDiff = (weekDifference * 5) + (dateToDay - dateFromDay);
    } else {
        dateDiff = ((weekDifference + 1) * 5) - (dateFromDay - dateToDay);
    }

    // fix: remove one day if it's saturday or sunday
    if (dateFromDayOrig >= 6 || dateFromDayOrig == 0) {
    dateDiff--;
    }

    return (dateDiff + 1); // add 1 because dates are inclusive

There seems to be few issues with the response that has been marked as solution.

  • The statement setFullYear() is returning incorrect value if I choose start date as 06/11/2015. So instead, the startDate1 and endDate1 can be directly passed to the function.

  • If the start date is Saturday or Sunday, still the code is counting it(iWeekday1) as 5 days

  • If the end date is Saturday or Sunday, still the code is counting it(iWeekday2) as 5 days. But these 5 days already get counted in the iweeks calculation.
    So instead of
    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    it should be
    iWeekday1 = (iWeekday1 > 5) ? 0 : iWeekday1; // only count weekdays
    iWeekday2 = (iWeekday2 > 5) ? 0 : iWeekday2;

  • The last IF condition should be executed when start and end date day is same like both are on same day, the date could be different
    if (iWeekday1 <= iWeekday2)

  • The condition that adjusts if both days are weekends can be removed
    iDateDiff -= iAdjust

  • Lastly, the +1 should be done only if start and end date falls on weekdays. Currently, it is adding in both the cases.
    return (iDateDiff + 1);//Add condition to apply only if both days are weekdays

--can't comment on that answer as I do not have that reputation :)

I have used Angular framework and Moment.js library to implement the solution. My solution covers all the cases.

this.daysInBetween = this.endMoment.diff(this.startMoment, 'days') + 1;
this.weeksInBetween = this.endMoment.diff(this.startMoment, 'weeks');
this.weekDays = this.daysInBetween - (this.weeksInBetween * 2);

if ( (this.startMoment.day() === 0 && this.endMoment.day() === 6) ||
 (this.startMoment.day() > this.endMoment.day()) ) {
  // IF ONE WEEKEND WAS MISSED
  this.weekDays-=2;
} else if ( this.startMoment.day() <= this.endMoment.day() &&
( this.startMoment.day() === 0 || this.startMoment.day() === 6 ||
  this.endMoment.day() === 0 || this.endMoment.day() === 6) ) {
  // IF EITHER OF DAYS WAS A WEEKEND
  this.weekDays--;
}

Live Demo: Calculate number of weekdays

I am currently working on a blog to write about my approach to this specific problem. I will post the link to the blog on the comment.

Important: Most answers here don't actually work if the start date (or sometimes the end date) is a saturday or sunday. For example: if your start and end date are

  • Saturday to Sunday or vice versa
  • or Saturday to Saturday
  • or Sunday to Sunday

So, here is the modified answer from the accepted answer

function calculateBusinessDays(dDate1, dDate2) {

var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) {
    return -1;
}

var iWeekday1 = dDate1.getDay();
var iWeekday2 = dDate2.getDay();

iWeekday1 = (iWeekday1 === 0) ? 7 : iWeekday1;
iWeekday2 = (iWeekday2 === 0) ? 7 : iWeekday2;

if (iWeekday1 > 5 && iWeekday2 <= 6) {
    iWeekday1 = 0;
    iAdjust = 1;
}
iWeekday2 = (iWeekday1 === 0 && iWeekday2 === 6) ? 0 : iWeekday2;

if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;

iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1;
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000);

if (iWeeks===0 && iWeekday1===0 && iWeekday2===0 
    && (dDate2.getTime() !== dDate1.getTime()) ) {
    iWeeks = 1;
}

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

}

Related