Get First and Last Day of the current Week in JavaScript

avatar

Borislav Hadzhiev

Last updated: Aug 8, 2022

banner

Photo from Unsplash

Get First and Last Day of the current Week in JavaScript #

To get the first and last days of the current week, use the setDate() method to change the day of the month of a date to the first and last days of the current week and pass the result to the Date() constructor.

index.js
const today = new Date(); const firstDay = new Date(today.setDate(today.getDate() - today.getDay())); const lastDay = new Date(today.setDate(today.getDate() - today.getDay() + 6)); console.log(firstDay); // 👉️ Sunday August 7 2022 console.log(lastDay); // 👉️ Saturday August 13 2022

The code snippet above considers Sunday to be the first day of the week and Saturday to be the last.

If your use case needs Monday to be the first day and Sunday to be the last, use this code snippet instead.

index.js
const today = new Date(); function getFirstDayOfWeek(d) { // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.getDay(); // 👉️ get day of week // 👇️ day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return new Date(date.setDate(diff)); } const firstDay = getFirstDayOfWeek(today); const lastDay = new Date(firstDay); lastDay.setDate(lastDay.getDate() + 6); console.log(firstDay); // 👉️ Monday August 8 2022 console.log(lastDay); // 👉️ Sunday August 14 2022

The getDate() method returns an integer between 1 and 31 that represents the day of the month for the given Date.

The getDay() method returns an integer between 0 and 6 that represents the day of the week for the given date: 0 is Sunday, 1 is Monday, 2 is Tuesday, etc.

The day of the month for the first day of the week is equal to day of the month - day of the week. If you consider Monday to be the first day of the week, add 1 to the result.

The day of the month for the last day of the week is equal to first day of the week + 6.

The last step is to use the setDate method, which takes the day of the month as a parameter and changes the value on the specific date.

The setDate() method returns the number of milliseconds between January 1st, 1970 and the given date.

To get a new Date object that represents the first day of the week, we pass the timestamp to the Date() constructor.

We repeated the same process to create a Date object that stores the last day of the week.

If you consider the first day of the week to be Monday:

  1. Subtract the day of the week from the day of the month.
  2. If the day of the week is Sunday, subtract 6 to get Monday.
  3. If it is any other day, add 1 because the getDay method returns a zero-based value.
index.js
const today = new Date(); function getFirstDayOfWeek(d) { // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.getDay(); // 👉️ get day of week // 👇️ day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return new Date(date.setDate(diff)); } const firstDay = getFirstDayOfWeek(today); const lastDay = new Date(firstDay); lastDay.setDate(lastDay.getDate() + 6); console.log(firstDay); // 👉️ Monday August 8 2022 console.log(lastDay); // 👉️ Sunday August 14 2022
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.