Skip to main content

How to Check if a Date is a Weekend or a Specific Weekday in JavaScript

When working with dates, a common requirement is to determine the day of the week. You might need to check if a date falls on a weekend to apply different business logic, or verify that a specific date is a Monday to schedule a weekly report. The JavaScript Date object has a simple, built-in method for this: .getDay().

This guide will teach you how to use the .getDay() method to find the day of the week as a number. You will then learn how to use this number to create simple and reusable functions to check if a date is a weekend, a weekday, or any specific day like Monday.

The Core Method: getDay()

The Date.prototype.getDay() method is the foundation for all these checks. It returns an integer between 0 and 6 that corresponds to the day of the week for a given date.

The mapping is:

  • 0: Sunday
  • 1: Monday
  • 2: Tuesday
  • 3: Wednesday
  • 4: Thursday
  • 5: Friday
  • 6: Saturday
const myDate = new Date('2023-10-27');  // This is a Friday
console.log(myDate.getDay()); // Output: 5
note

By checking the number returned by this method, we can determine any day of the week.

Solution 1: Check if a Date is a Weekend

A weekend consists of Saturday and Sunday. Using the .getDay() mapping, this means we need to check if the day's number is 6 (Saturday) or 0 (Sunday).

function isWeekend(date) {
const day = date.getDay();
return day === 0 || day === 6;
}

const saturday = new Date('2023-10-28');
console.log(`Is Saturday a weekend? ${isWeekend(saturday)}`);
// Output: Is Saturday a weekend? true

const sunday = new Date('2023-10-29');
console.log(`Is Sunday a weekend? ${isWeekend(sunday)}`);
// Output: Is Sunday a weekend? true

const monday = new Date('2023-10-30');
console.log(`Is Monday a weekend? ${isWeekend(monday)}`);
// Output: Is Monday a weekend? false

This simple and readable function is the standard way to solve this problem.

Solution 2: Check if a Date is a Weekday

A weekday is any day that is not a weekend (Monday through Friday). We can create this check by simply inverting the logic from our isWeekend function.

function isWeekend(date) {
const day = date.getDay();
return day === 0 || day === 6;
}

function isWeekday(date) {
return !isWeekend(date);
}

const friday = new Date('2023-10-27');
console.log(`Is Friday a weekday? ${isWeekday(friday)}`);
// Output: Is Friday a weekday? true

const saturday = new Date('2023-10-28');
console.log(`Is Saturday a weekday? ${isWeekday(saturday)}`);
// Output: Is Saturday a weekday? false
note

By reusing our isWeekend function, we keep our code clean and "DRY" (Don't Repeat Yourself).

Solution 3: Check if a Date is a Specific Day (e.g., Monday)

To check for any specific day, you just need to compare the result of .getDay() to the corresponding number from the mapping.

function isMonday(date) {
return date.getDay() === 1;
}

const monday = new Date('2023-10-30');
console.log(`Is this date a Monday? ${isMonday(monday)}`);
// Output: Is this date a Monday? true

const tuesday = new Date('2023-10-31');
console.log(`Is this date a Monday? ${isMonday(tuesday)}`);
// Output: Is this date a Monday? false
note

You can easily adapt this function for any other day of the week (e.g., isFriday would check for === 5).

Conclusion

Determining the day of the week in JavaScript is a simple task thanks to the built-in .getDay() method.

The key takeaways are:

  1. The .getDay() method is the core tool. It returns a number from 0 (Sunday) to 6 (Saturday).
  2. To check if a date is a weekend, check if the day is 0 or 6.
  3. To check if a date is a weekday, check if the day is not 0 and not 6.
  4. To check for a specific day, compare the result of .getDay() to its corresponding number in the standard mapping.

By creating small, reusable functions for these checks, you can write clean, readable, and maintainable date-based logic.