Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if weekend exist in date range using javascript

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")
    } 
}
like image 342
BaconJuice Avatar asked Jun 04 '14 13:06

BaconJuice


People also ask

How do you know if a date is weekend in JS?

Use the getDay() method to check if a date is during the weekend, e.g. date. getDay() === 6 || date. getDay() === 0 . The method returns a number between 0 and 6 for the day of the week, where Sunday is 0 and Saturday is 6 .

How do you check if a date is within a date range Javascript?

You can check if a date is between two dates by simply using the >= and <= operators. Typescript doesn't like this approach and will complain. To make Typescript happy, use the valueOf() function in conjunction with Date() .

How do you check if the day is a weekday in Javascript?

Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

How do you check if the date is in between two dates?

To check if a date is between two dates:Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.


1 Answers

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;
}
like image 173
Abhitalks Avatar answered Sep 19 '22 18:09

Abhitalks