2

I'm trying to figure out if there is a way to figure how many weekend days exist between two date ranges.

Here is currently what I have in JS

function isWeekend(date1, date2) {
    var d1 = new Date(date1),
        d2 = new Date(date2), 
        weekendDays = 0;

    while (d1 < d2) {
        var day = d1.getDay();
        if(day == 6 || day == 0){
            weekendDays++;
        }
        d1.setDate(d1.getDate() + 1);
    }
    return weekendDays;
}

Currently for some reason regardless of what date range it is, it seems to keep returning 1

What I want to basically achieve is that if the date range have Sunday it returns 1 and if it has Saturday AND Sunday it returns 2 and if it just has Saturday it returns 1.

Can someone lend me a helping hand to figure this out?

Thank you for taking your time to read this.

BaconJuice
  • 3,739
  • 13
  • 56
  • 88
  • Do you want to be inclusive or not? (d1 <= d2) instead of (d1 < d2) If you pass only a range of two days (saturday and sunday) you'll get 1 instead of 2. – ForguesR Jun 11 '14 at 18:16
  • @ForguesR I added a modified fiddle here http://jsfiddle.net/vg8DG/ Currently it shows 3 when it should be only showing 2. Any ideas? – BaconJuice Jun 11 '14 at 18:20
  • 1
    In fact your problem is related to this http://stackoverflow.com/questions/1208519/javascript-date-objects-month-index-begins-with-0. (You are using July, not June!) – ForguesR Jun 11 '14 at 18:35

0 Answers0