How to Get the First and Last Day of the Week in JavaScript
A common requirement in date pickers, calendars, and reporting applications is to find the start and end dates of the current week. However, the definition of "start of the week" varies culturally—in many regions, it's Monday, while in others, it's Sunday.
This guide will teach you how to create a single, powerful, and reusable function to get the first and last day of the week for any given date. You will learn how to parameterize this function to handle weeks starting on either Sunday or Monday.
The Core Logic: Using getDay() and setDate()
The logic for finding the start of the week involves a simple calculation based on the current day of the week.
- Get the Current Date: Start with a
Dateobject (e.g.,new Date()). - Get the Day of the Week: Use the
date.getDay()method. This returns a number where 0 is Sunday, 1 is Monday, ..., 6 is Saturday. - Calculate the Difference: Subtract the day-of-the-week index from the current day of the month. This calculation tells you how many days to "go back" to find the start of the week.
- Set the New Date: Use the
date.setDate()method with the calculated difference to get the first day of the week.
The Reusable Function (Best Practice)
For clean and maintainable code, it's best to encapsulate this logic in a function that can be adapted for weeks starting on Sunday or Monday.
/**
* Gets the first and last day of the week for a given date.
* @param {Date} date The date to get the week for.
* @param {object} [options] Optional configuration.
* @param {boolean} [options.weekStartsOnMonday=false] If true, the week starts on Monday.
* @returns {{firstDay: Date, lastDay: Date}} An object containing the first and last day.
*/
function getWeek(date, options = {}) {
const { weekStartsOnMonday = false } = options;
const today = new Date(date.getTime()); // Clone the date
const day = today.getDay(); // 0=Sunday, 1=Monday, ...
// Calculate the difference to the first day of the week
let diff;
if (weekStartsOnMonday) {
diff = today.getDate() - day + (day === 0 ? -6 : 1);
} else {
diff = today.getDate() - day;
}
// Set the date to the first day of the week
const firstDay = new Date(today.setDate(diff));
// The last day is 6 days after the first day
const lastDay = new Date(firstDay);
lastDay.setDate(firstDay.getDate() + 6);
return { firstDay, lastDay };
}
Practical Examples
With our getWeek function defined above, getting the start and end of the week for any locale becomes trivial.
How to Get a Week That Starts on Sunday
This is the default behavior of our function, common in regions like the United States.
However, we need to find the Sunday and Saturday of the current week.
Solution:
const today = new Date();
// By default, the week starts on Sunday
const { firstDay, lastDay } = getWeek(today);
console.log('First day (Sunday):', firstDay.toDateString());
console.log('Last day (Saturday):', lastDay.toDateString());
How to Get a Week That Starts on Monday
This is common in many parts of Europe and other regions. We just need to pass the option to our function.
Problem: we need to find the Monday and Sunday of the current week.
Solution:
const today = new Date();
// Pass the option to start the week on Monday
const { firstDay, lastDay } = getWeek(today, { weekStartsOnMonday: true });
console.log('First day (Monday):', firstDay.toDateString());
console.log('Last day (Sunday):', lastDay.toDateString());
Conclusion
Finding the first and last day of the week is a common date manipulation task that can be solved with a single, robust function.
- The core logic relies on the
getDay()method to find the current day's position in the week andsetDate()to calculate the start date. - The best practice is to create a reusable function that is parameterized to handle different start-of-the-week conventions (Sunday vs. Monday).
- Always clone your input
Dateobject (e.g.,new Date(date.getTime())) before performing mutations to avoid unexpected side effects.
By using this modular approach, you can handle weekly date calculations cleanly and reliably in any application.