7

Wondering if anyone has a solution for checking if a weekend exist between two dates and its range.

var date1 = 'Apr 10, 2014';
var date2 = 'Apr 14, 2014';


funck isWeekend(date1,date2){
   //do function

    return isWeekend;
}

Thank you in advance.

EDIT Adding what I've got so far. Check the two days.

function isWeekend(date1,date2){
   //do function
    if(date1.getDay() == 6 || date1.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
        if(date2.getDay() == 6 || date2.getDay() == 0){
        return isWeekend;
        console.log("weekend")
    } 
}
BaconJuice
  • 3,739
  • 13
  • 56
  • 88
  • 6
    show us what you tried ? – Charaf JRA Jun 04 '14 at 13:38
  • Calculate the number of days between you start and end date, if it's more than `5`, then it must contain a weekend day. (it's not clear if you want a *whole* weekend, or just a Saturday or a Sunday). If it's `<5` days, then you need to use `getDay()` on the start and end date. This ranges from `0` for Sunday to `6` for Saturday, so again it should be easy to figure out if those days will fall in that range. – Matt Burland Jun 04 '14 at 13:49
  • There's an example [here](http://stackoverflow.com/a/14655646/1906094) for you to check how too loop through 2 dates and in that loop you can check `start.getDay()`, value to determine if there's a weekend or not. – Batu.Khan Jun 04 '14 at 13:49
  • @BatuZet: I don't think you even need to loop. If you do `getDay()` on the start and the end, you should be able to work it out. – Matt Burland Jun 04 '14 at 13:50
  • Yeah @Matt Burland . I posted mine just after miliseconds you did. If i saw yours earlier, i wouldn't post mine :P – Batu.Khan Jun 04 '14 at 13:52
  • @MattBurland that's what I've done so far and I don't think it's working right since it only check the two dates and nothing in between them. Also sorry let me post what I have so far! – BaconJuice Jun 04 '14 at 13:52

7 Answers7

11

Easiest would be to just iterate over the dates and return if any of the days are 6 (Saturday) or 0 (Sunday)

Demo: http://jsfiddle.net/abhitalks/xtD5V/1/

Code:

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

    while (d1 < d2) {
        var day = d1.getDay();
        isWeekend = (day === 6) || (day === 0); 
        if (isWeekend) { return true; } // return immediately if weekend found
        d1.setDate(d1.getDate() + 1);
    }
    return false;
}

If you want to check if the whole weekend exists between the two dates, then change the code slightly:

Demo 2: http://jsfiddle.net/abhitalks/xtD5V/2/

Code:

function isFullWeekend(date1, date2) {
    var d1 = new Date(date1),
        d2 = new Date(date2); 

    while (d1 < d2) {
        var day = d1.getDay();
        if ((day === 6) || (day === 0)) { 
            var nextDate = d1; // if one weekend is found, check the next date
            nextDate.setDate(d1.getDate() + 1); // set the next date
            var nextDay = nextDate.getDay(); // get the next day
            if ((nextDay === 6) || (nextDay === 0)) {
                return true; // if next day is also a weekend, return true
            }
        }
        d1.setDate(d1.getDate() + 1);
    }
    return false;
}
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • can you explain to me what exactly is happening here? isWeekend = (day == 6) || (day == 0); Sorry just trying to understand the logic a bit more :) – BaconJuice Jun 04 '14 at 14:14
  • If day is `6` (i.e. Saturday) or if the day is `0` (i.e. Sunday), then we know it is a weekend day. (In Javascript days are 0 thru 6) We set the boolean variable `isWeekend` to true, and return if it is true. Alternatively you could directly return true. – Abhitalks Jun 04 '14 at 14:17
  • Ah! I had no idea you could do that directly in a variable in JS. That's pretty awesome! Thank you again! – BaconJuice Jun 04 '14 at 14:24
  • 1
    if date2 is a Saturday the function will return false you should use `while (d1 <= d2)` – cczak Mar 11 '21 at 06:51
4

You are only checking if the first or second date is a weekend day.

Loop from the first to the second date, returning true only if one of the days in between falls on a weekend-day:

function isWeekend(date1,date2){
    var date1 = new Date(date1), date2 = new Date(date2);

    //Your second code snippet implies that you are passing date objects 
    //to the function, which differs from the first. If it's the second, 
    //just miss out creating new date objects.

    while(date1 < date2){
        var dayNo = date1.getDay();
        date1.setDate(date1.getDate()+1)
        if(!dayNo || dayNo == 6){
            return true;
        }
    }
}

JSFiddle

George
  • 36,413
  • 9
  • 66
  • 103
1

Here's what I'd suggest to test if a weekend day falls within the range of two dates (which I think is what you were asking):

function containsWeekend(d1, d2)
{
    // note: I'm assuming d2 is later than d1 and that both d1 and d2 are actually dates
    // you might want to add code to check those conditions
    var interval = (d2 - d1) / (1000 * 60 * 60 * 24); // convert to days
    if (interval > 5) {
        return true;    // must contain a weekend day
    }
    var day1 = d1.getDay();
    var day2 = d2.getDay();
    return !(day1 > 0 && day2 < 6 && day2 > day1);
}

fiddle

If you need to check if a whole weekend exists within the range, then it's only slightly more complicated.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

It doesn't really make sense to pass in two dates, especially when they are 4 days apart. Here is one that only uses one day which makes much more sense IMHO:

var date1 = 'Apr 10, 2014';

function isWeekend(date1){
  var aDate1 = new Date(date1);
  var dayOfWeek = aDate1.getDay();

  return ((dayOfWeek == 0) || (dayOfWeek == 6));
}
Ricky Nelson
  • 876
  • 10
  • 24
0

I guess this is the one what @MattBurland sugested for doing it without a loop

function isWeekend(start,end){
  start = new Date(start);
  if (start.getDay() == 0 || start.getDay() == 6) return true;
  end = new Date(end);

  var day_diff = (end - start) / (1000 * 60 * 60 * 24);
  var end_day = start.getDay() + day_diff;    
  if (end_day > 5) return true;

  return false;
}

FIDDLE

Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
0

Whithout loops, considering "sunday" first day of week (0):

Check the first date day of week, if is weekend day return true.

SUM "day of the week" of the first day of the range and the number of days in the lap. If sum>5 return true

miguel-svq
  • 2,136
  • 9
  • 11
0

Use Date.getDay() to tell if it is a weekend.

  if(tempDate.getDay()==6 || tempDate.getDay()==0)

Check this working sample:

http://jsfiddle.net/danyu/EKP6H/2/

This will list out all weekends in date span. Modify it to adapt to requirements. Good luck.

Danyu
  • 509
  • 3
  • 7