Check if a Date is Friday using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 25, 2022

banner

Check out my new book

Table of Contents #

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

Check if a Date is a Friday using JavaScript #

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

index.js
function isFriday(date = new Date()) { return date.getDay() === 5; } const d1 = new Date('2022-09-23'); console.log(d1); // 👉️ Friday Sep 23 2022 console.log(d1.getDay()); // 👉️ 5 console.log(isFriday(d1)); // 👉️ true const d2 = new Date('2022-09-22'); console.log(d2); // 👉️ Thu Sep 22 2022 console.log(d2.getDay()); // 👉️ 4 console.log(isFriday(d2)); // 👉️ false

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

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 Friday is 5, all we have to do is check if calling the getDay method on the date returns 5.

Check if a Date is the Friday of the Current week #

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

  1. Get the date for the Friday of the current week.
  2. Use the toDateString() method to compare Friday to the passed in date.
  3. If the method returns 2 equal strings, then the passed in date is the Friday of the current week.
index.js
function isFridayOfCurrentWeek(date = new Date()) { const today = new Date(); const first = today.getDate() - today.getDay() + 1; const fifth = first + 4; const friday = new Date(today.setDate(fifth)); return friday.toDateString() === date.toDateString(); } const today = new Date(); const first = today.getDate() - today.getDay() + 1; const fifth = first + 4; const currentFriday = new Date(today.setDate(fifth)); console.log(isFridayOfCurrentWeek(currentFriday)); // 👉️ true const d2 = new Date('2022-09-22'); console.log(d2); // 👉️ Thu Sep 22 2022 console.log(d2.getDay()); // 👉️ 4 console.log(isFridayOfCurrentWeek(d2)); // 👉️ false

To get the Friday of the current week, we had to calculate the day of the month of the Monday of the current week and add 4 to the result.

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