1

i have to calculate the weekend Count and holidays count between the following two dates.

var startDate = new Date("01/02/2014");
        var endDate = new Date("02/06/2014");
        var holidays = [new Date("01/06/2014"), new Date("01/26/2014")];
Raja
  • 209
  • 1
  • 7
  • 16
  • 3
    So what's your question? – John Conde Jan 28 '14 at 18:32
  • 1
    Check out Momentjs (http://momentjs.com/) or Sugarjs (http://sugarjs.com/dates) – xspydr Jan 28 '14 at 18:32
  • without iteration, you mean not iterating from startDate to endDate? – J. Rahmati Jan 28 '14 at 18:43
  • i have check this moment but i can't bind the logic for this weekend count calculation – Raja Jan 28 '14 at 18:44
  • yes Mr.J.Rahmati is it possible to calculate without iteration between two different date; – Raja Jan 28 '14 at 18:55
  • @Raja, For start and endDate I agree, but I can't see how that works for holidays except with some library which will use iteration in the background anyway. – J. Rahmati Jan 28 '14 at 18:57
  • Thanks for your response. how to calculate the weekendCount without iteration – Raja Jan 28 '14 at 19:01
  • I think the script on the following [page](http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days) could be modified for your needs (weekendDays = allDayDifference - businessdays) – J. Rahmati Jan 28 '14 at 19:50

3 Answers3

2

This loops through a partial week, so there are at most 6 iterations. I can't think of a more elegant way to solve that with pure JS.

var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");

var diff = Math.abs(startDate - endDate); // in milliseconds
var ms_per_day = 1000*60*60*24;
var days = diff/ms_per_day + 1; // convert to days and add 1 for inclusive date range
var mod = days % 7;
var full_weeks = (days - mod) / 7;

var weekend_days = full_weeks * 2;

if (mod != 0) { // iterate through remainder days
    var startPartialWeek = new Date();
    var endPartialWeek = endDate;
    startPartialWeek.setTime(endDate.getTime() - (mod - 1)*ms_per_day);
    for (var d = startPartialWeek; d <= endPartialWeek; d.setDate(d.getDate() + 1)) {
        if(d.getDay() == 0 || d.getDay() == 6) {
            weekend_days++;
        }
    }
}

alert(weekend_days);

This counts Saturdays and Sundays only, not holidays. I don't think you'll be able to do holidays without iterating through a collection of holiday dates you get from some other source.

Natalie Chouinard
  • 1,472
  • 1
  • 10
  • 15
1

Having following two functions:

function calculateTotalDays(firstDate, secondDate){
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var firstDate = new Date(2008,01,12);
    var secondDate = new Date(2008,01,22);

    var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
    return diffDays;
}

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
}

You can calculate total weekenddays as follows:

var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var totalDays = calculateTotalDays(startDate, endDate);
var weekendDays = totalDays - calcBusinessDays(startDate, endDate);

And then count holidays in between start and endDate:

var totalHolidays = 0;
for (var i = 0, i < holidays.length; i++){
  var d = holidays[i].getDay();//Make sure holiday is not a weekendday!
  if (holidays[i] >= startDate && holidays[i] <= endDate && !(d == 0 || d==6))
    totalHolidays++;
}
J. Rahmati
  • 735
  • 10
  • 37
  • As additional info calcBusinessDays is the function from this [page](http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days)! I didn't write it myself! – J. Rahmati Jan 28 '14 at 20:22
  • This fails if the start day is a "sunday" and the end one is a "friday". It only counts 1 day. For example, from the 18th of January of 2015 to the 23rd of January of 2015. [Here's the reproduction of the bug](http://jsfiddle.net/e6gmkLwg/) – Alvaro Feb 19 '15 at 11:57
0
// Count days between the 2 dates
var days = Math.floor(((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())
  - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (24 * 60 * 60 * 1000)));

// Count holidays
var countHolidays = 0;
for (var i = 0, len = holidays.length; i < len; ++i)
  if (holidays[i] >= startDate && holidays[i] <= endDate)
    ++countHolidays;

// Vars used to count sundays and saturdays
var adjustingDays1 = (7 - startDate.getDay()) % 7, // days between week of startDate and sunday
    adjustingDays2 = (7 + 6 - startDate.getDay()) % 7, // days between week of startDate and saturday
    oddDays = days % 7; // remainder of total days after dividing 7
    completeWeeks = Math.floor(days / 7);

// Count weekend days
var countWeekEndDays = completeWeeks + (oddDays >= adjustingDays1 ? 1 : 0) +
           completeWeeks + (oddDays >= adjustingDays2 ? 1 : 0);

console.log('holidays: ' + countHolidays);
console.log('weekend days: ' + countWeekEndDays);
gtournie
  • 4,143
  • 1
  • 21
  • 22