How to Get the Current Week’s Start and End Date with JavaScript?

To get the current week’s start and end date, call the getDate() and getDay() methods on the current date object to get the day of the month and the day of the week respectively and then pass their difference to the setDate() method to get the current week’s start date and their difference + 6 to get the current week’s end date.

For example,

// Get current date
const today = new Date();

// Get the difference
const diff = today.getDate() - today.getDay();

// Get the current week's start date
const startDate = new Date(today.setDate(diff));

console.log(startDate);
// Output: 👉 Sun Mar 12 2023 12:37:06 GMT+0530 (India Standard Time)

// Get the current week's end date
const endDate = new Date(today.setDate(diff + 6));

console.log(endDate);
// Output: 👉 Sat Mar 18 2023 12:37:06 GMT+0530 (India Standard Time)

The getDate() method returns an integer between 1 and 31 representing the day of the month. If you call it on the current date object, it returns today’s date.

The getDay() method on the other hand returns an integer between 0 and 6 representing the day of the week. The value 0 represents Sunday, 1 represents Monday, and so on.

For example,

// current date
const today = new Date();

console.log(today.getDate());
// Output: 👉 18

console.log(today.getDay());
// Output: 👉 6

To get the first day of the current week, we pass the difference of the getDate() and getDay() methods to the setDate() method.

The setDate() method sets the day of the month on a given date object.

For example,

// current date
const today = new Date();

console.log(today);
// Output: 👉 Sat Mar 18 2023 12:52:23 GMT+0530 (India Standard Time)

// Set date to the first day of month
today.setDate(1);

console.log(today);
// Output: 👉 Wed Mar 01 2023 12:53:02 GMT+0530 (India Standard Time)

Get the Current Week’s Start and End Date Considering Monday as First Day

In the last example, we considered Sunday as the first day and Saturday as the last day of the week.

But if you want the week to start on Monday and end on Sunday, you have to put a conditional check on our previous logic.

For example,

// Get current date
const today = new Date();

// Get day of week
const day = today.getDay();

// Get the difference
const diff = today.getDate() - day + (day === 0 ? -6: 1); 

// Get the current week's start date
const startDate = new Date(today.setDate(diff));

console.log(startDate);
// Output: 👉 Mon Mar 13 2023 13:22:16 GMT+0530 (India Standard Time)

// Get the current week's end date
const endDate = new Date(today.setDate(diff + 6));

console.log(endDate);
// Output: 👉 Sun Mar 19 2023 13:22:16 GMT+0530 (India Standard Time)

In this example, While getting the difference of the getDate() and getDay() methods,

  • We add -6 to the diff if the current day is Sunday so that the first day could become Monday
  • We add +1 to the diff if the current day is not Sunday

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.