Check if a Date is during the Weekend using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Table of Contents

  1. Check if a Date is during the Weekend using JavaScript
  2. Check if a Date is during the Weekend using modulo % operator
  3. Check if a Date is a Monday using JavaScript
  4. Check if a Date is the Monday of the Current Week

# Check if a Date is during the Weekend using JavaScript

Use the getDay() method to check if a date is during the weekend.

The method returns a number between 0 and 6 for the day of the week, where Sunday is 0 and Saturday is 6.

index.js
function isWeekend(date = new Date()) { return date.getDay() === 6 || date.getDay() === 0; } const d1 = new Date('2022-09-24'); console.log(d1); // ๐Ÿ‘‰๏ธ Saturday Sep 24 2022 console.log(d1.getDay()); // ๐Ÿ‘‰๏ธ 6 console.log(isWeekend(d1)); // ๐Ÿ‘‰๏ธ true const d2 = new Date('2022-09-23'); console.log(d2); // ๐Ÿ‘‰๏ธ Friday Sep 23 2022 console.log(isWeekend(d2)); // ๐Ÿ‘‰๏ธ false

check if date is during weekend

The code for this article is available on GitHub
We created a reusable function that takes a Date object as a parameter and checks if the date is during the weekend.

If you don't pass a Date object to the function, it uses the current date.

The Date.getDay() method returns a number between 0 and 6 that represents the day of the week for the given date.

The method returns 0 for Sunday, 1 for Monday, 2 for Tuesday, etc.

Since we know the values for Sunday and Saturday are 0 and 6, all we have to do is check if calling the getDay method on the date returns either of the two values.

The logical OR (||) operator returns the value to the right if the value to the left is falsy, so if either condition is met, the function returns true.

# Check if a Date is during the Weekend using modulo % operator

An alternative approach is to check if dividing the result of calling the getDay() method by 6 has a remainder.

index.js
function isWeekend(date = new Date()) { return date.getDay() % 6 === 0; } const d1 = new Date('2022-09-24'); console.log(d1); // ๐Ÿ‘‰๏ธ Saturday Sep 24 2022 console.log(isWeekend(d1)); // ๐Ÿ‘‰๏ธ true const d2 = new Date('2022-09-23'); console.log(d2); // ๐Ÿ‘‰๏ธ Friday Sep 23 2022 console.log(isWeekend(d2)); // ๐Ÿ‘‰๏ธ false

check if date is during weekend using modulo operator

The code for this article is available on GitHub

The modulo (%) operator returns the remainder of the division.

index.js
console.log(6 % 6); // ๐Ÿ‘‰๏ธ 0 console.log(0 % 6); // ๐Ÿ‘‰๏ธ 0

Dividing 6 and 0 by 6 gives us a remainder of 0, so the condition will only be met if the date is Saturday or Sunday.

# Check if a Date is a Monday using JavaScript

You can also use the getDay() method to check if a day is Monday.

The method returns a number between 0 and 6 for the day of the week, where Monday is 1.

index.js
function isMonday(date = new Date()) { return date.getDay() === 1; } const d1 = new Date('2022-09-19'); console.log(d1); // ๐Ÿ‘‰๏ธ Mon Sep 19 2022 console.log(d1.getDay()); // ๐Ÿ‘‰๏ธ 1 console.log(isMonday(d1)); // ๐Ÿ‘‰๏ธ true const d2 = new Date('2022-09-20'); console.log(d2); // ๐Ÿ‘‰๏ธ Tue Sep 20 2022 console.log(d2.getDay()); // ๐Ÿ‘‰๏ธ 2 console.log(isMonday(d2)); // ๐Ÿ‘‰๏ธ false

check if date is monday

The code for this article is available on GitHub

We created a reusable function that takes a Date object as a parameter and checks if the date is a Monday.

If you don't pass a Date object to the function, it uses the current date.

The Date.getDay() method returns a number between 0 and 6 that represents the day of the week for the given date.

Since we know the value for Monday is 1, all we have to do is check if calling the getDay method on the date returns 1.

# Check if a Date is the Monday of the Current Week

To check if a date is the Monday of the current week:

  1. Get the date for the Monday of the current week.
  2. Use the toDateString() method to compare Monday to the passed-in date.
  3. If the method returns 2 equal strings, then the passed-in date is the Monday of the current week.
index.js
function isMondayOfCurrentWeek(date = new Date()) { const today = new Date(); const first = today.getDate() - today.getDay() + 1; const monday = new Date(today.setDate(first)); return monday.toDateString() === date.toDateString(); } const today = new Date(); const first = today.getDate() - today.getDay() + 1; const currentMonday = new Date(today.setDate(first)); console.log(isMondayOfCurrentWeek(currentMonday)); // ๐Ÿ‘‰๏ธ true const date = new Date('2022-09-24'); console.log(date); // ๐Ÿ‘‰๏ธ Sat Sep 24 2022 console.log(isMondayOfCurrentWeek(date)); // ๐Ÿ‘‰๏ธ false

check if date is monday of current week

The code for this article is available on GitHub

To get the Monday of the current week, we had to calculate the day of the month for Monday.

We basically subtracted the value for the day of the week from the day of the month and added 1 to get the Monday.

The toDateString() returns a string representing the date portion of the given Date instance in human-readable form.

index.js
// ๐Ÿ‘‡๏ธ Tue Jan 25 2022 console.log(new Date().toDateString());

If the output from calling toDateString() for the two dates is the same, then the date is the Monday of the current week.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev