Last updated: Mar 5, 2024
Reading timeยท3 min
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.
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
The code sample 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.
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
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.
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:
6
to get Monday.1
because the getDay
method returns a
zero-based value.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
You can learn more about the related topics by checking out the following tutorials: