Get First and Last Day of the current Week in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# Table of Contents

  1. Get First (Sunday) and Last Day (Saturday) of the current Week
  2. Get the First (Monday) and Last Day (Sunday) of the current Week

# Get First (Sunday) and Last Day (Saturday) of the current Week

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(); // โœ… Get the first day of the current week (Sunday) const firstDay = new Date( today.setDate(today.getDate() - today.getDay()), ); // โœ… Get the last day of the current week (Saturday) const lastDay = new Date( today.setDate(today.getDate() - today.getDay() + 6), ); console.log(firstDay); // ๐Ÿ‘‰๏ธ Sun Jul 23 2023 14:38:23 console.log(lastDay); // ๐Ÿ‘‰๏ธ Sat Jul 29 2023 14:38:23

get first and last day of current week

The code for this article is available on GitHub

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

# Get the First (Monday) and Last Day (Sunday) of the current Week

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(); // โœ… Get the first day of the current week (Monday) 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); // โœ… Get the last day of the current week (Sunday) const lastDay = new Date(firstDay); lastDay.setDate(lastDay.getDate() + 6); console.log(firstDay); // ๐Ÿ‘‰๏ธ Mon Jul 24 2023 14:40:17 console.log(lastDay); // ๐Ÿ‘‰๏ธ Sun Jul 30 2023 14:40:17

get first monday and last sunday of current week

The code for this article is available on GitHub

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

The Date.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 Date.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(); // โœ… Get the first day of the current week (Monday) 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); // โœ… Get the last day of the current week (Sunday) 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 code for this article is available on GitHub

# 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