Check if Date is Monday using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 25, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Check if a Date is a Monday using JavaScript
  2. Check if a Date is the Monday of the Current week

Check if a Date is a Monday using JavaScript #

Use the getDay() method to check if a date is a Monday, e.g. date.getDay() === 1. 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

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 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 Friday 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

To get the Monday of the current week, we had to calculate the day of the month for the 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.

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.