Check if a Date is a Weekend using JavaScript

To check if a date is a weekend i.e. Saturday or Sunday, you can use the getDay() method of the Date() object. The getDay() method returns an integer between 0 and 6 representing the day of the week, where 0 represents Sunday, 1 represents Monday and so on up to 6(Saturday).

So, we have to basically check if the value returned by the getDay() method is 0 or 6. If it is 0, it means the day is Sunday and if it is 6, it means the day is Saturday i.e. the given date is a weekend.

// Function to check if a date is weekend
function isWeekend(date){
    if(date.getDay() === 0 || date.getDay() === 6){
        return true;
    }
    else{
        return false;
    }
}

let date1 = new Date('2022-11-26');
console.log(date1);  // Sat Nov 26 2022

console.log(isWeekend(date1));  // true

let date2 = new Date('2022-11-27');
console.log(date2);  // Sun Nov 27 2022

console.log(isWeekend(date2));  // true

let date3 = new Date('2022-11-28');
console.log(date3);  // Mon Nov 28 2022

console.log(isWeekend(date3));  // false

You can see from the above example that the isWeekend() function returns true only if the given date is a weekend i.e. either Sunday or Saturday. It returns false for all dates that are weekdays.


Check if Today is Weekend?

In JavaScript, if we want to get the today’s date, we can simply call the Date() method without passing any parameters to it. This gives us the today’s date.

For eg.

// Returns today's date
const today = new Date();

console.log(today);
// Thu Nov 24 2022 20:54:59 ...

Now, if we want to check if today is a weekend or a weekday, we can use the similar approach i.e. call the getDay() method on the date object returned by the Date() method. If the returned value is 0(Sunday) or 6(Saturday), today is the weekend, otherwise, it’s a weekday.

Example:

// Function to check if given date is weekend
function isWeekend(date){
    if(date.getDay() === 0 || date.getDay() === 6){
        return true;
    }
    else{
        return false;
    }
}

// Returns today's date
const today = new Date();

console.log(today);    // Thu Nov 24 2022 21:01:48
console.log(isWeekend(today));  // false

The day I ran this code is a weekday(Thursday), therefore, the isWeekend() function returned false.

Notice that we have used the Date() method with the new keyword. Therefore, it acts as a date constructor and returns the date as an object.

But you can also call it without the new keyword. In that case, it acts as a normal date function and returns the date as a string. Therefore, if you call any built-in date method such as getDay(), getMonth() etc. on the returned date, it gives you a TypeError.

Example:

// Date() used as a constructor
const date1 = new Date();

console.log(date1);    // Thu Nov 24 2022 21:16:06 ...
console.log(typeof date1);  // object


// Date() used as a function
const date2 = Date();

console.log(date2);   // Thu Nov 24 2022 21:16:06 ...
console.log(typeof date2);  // string

As you can see from the above example, when we used the Date() with the new keyword, the returned date is an object and when we used it without the new keyword, the returned date is a string.


Check if a Date is a Weekend using the toLocaleString() Method

The toLocaleString() method returns a language-specific representation of a given date. It also gives us some optional parameters to get the week-day name, month name, year representation etc. in long and short formats.

For eg.

const date = new Date('2022-11-26');

console.log(date.toLocaleString('en-US', {weekday: 'long'}));
// Output: Saturday

console.log(date.toLocaleString('en-US', {weekday: 'short'}));
// Output: Sat

As you can see, the toLocaleString() method returns the weekday name in long and short formats based on the parameters passed to it.

So, we can use this function directly to get the day name and then check if the day is Sunday or Saturday. If yes, the given date is a weekend, otherwise not.

Example:

function isWeekend(date){
    
    // Get the day name
    let dayName = date.toLocaleString('en-US', {weekday: 'long'});
    
    // Check if day is Sunday or Saturday
    if(dayName ==='Sunday' || dayName === 'Saturday'){
        return true;
    }
    else{
        return false;
    }
}

let date1 = new Date('2022-11-26');
console.log(date1);  // Sat Nov 26 2022

console.log(isWeekend(date1));  // true

let date2 = new Date('2022-11-27');
console.log(date2);  // Sun Nov 27 2022

console.log(isWeekend(date2));  // true

Conclusion

In this article, we learned two ways to check if the given date is a weekend or not.

The first approach is to create a date object from the given date and then call the getDay() method on this date object. If the getDay() method returns 0 or 6, the date is a weekend, otherwise not.

The second approach is to get the weekday name from the given date using the toLocaleString() method and then check if the returned day is Saturday or Sunday. If yes, the given date is a weekend, otherwise not.


Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.