How to Get First and Last Day of the Current Week in JavaScript

To Get the First and Last Day of the Current Week in JavaScript, you can use the “Date” object.

Here’s a step-by-step guide:

  1. Access the current index of the day of the week using the above-discussed getDay() function.
  2. Access the current date using the getDate() function.
  3. If we subtract the date from the day, we get the first day of the week. 
  4. For getting the last day of the week, we need to add 6 to the value.
  5. Keep in mind that Sunday is the first day of the week ‘0’, and Saturday is the last day of week ‘6’

Example

let date_today = new Date();

console.log(`The current day index of the week is: ${date_today.getDay()}`);

console.log(`The current date of the week is: ${date_today.getDate()}`);

let first_day_of_the_week = new Date(date_today.setDate(date_today.getDate() 
                               - date_today.getDay() ));

let last_day_of_the_week = new Date(date_today.setDate(date_today.getDate() 
                               - date_today.getDay() + 6));

console.log(`The first date of the week is: ${first_day_of_the_week}`); 

console.log(`The last day of the week is: ${last_day_of_the_week}`);

Output

The current day index of the week is: 1

The current date of the week is: 26

The first date of the week is: Sun Sep 25 2022 11:57:42 GMT+0530 (India Standard Time)

The last day of the week is: Sat Oct 01 2022 11:57:42 GMT+0530 (India Standard Time)

Explanation

The above code can be explained as follows:

  1. We first created the variable named date_today using the let keyword.
  2. Now we initialize the variable with the help of the new Date() constructor.
  3. We first printed the current day index of the week and the current date using the functions getDay() and getDate() functions, respectively.
  4. Then we created another variable named first_day_of_the_week and initialized it too. Also, we have subtracted the current day index from the current date. 
  5. We then used the setDate() function to set the obtained date.
  6. We did a similar operation of the last_day_of_the_week variable.
  7. Finally, we printed them.

But If you want Monday to be the first day of the week and Sunday to be the last day of the week, you can use below code snippet.

let date_today = new Date();


console.log(`The current day index of the week is: ${date_today.getDay()}`);


console.log(`The current date of the week is: ${date_today.getDate()}`);


let first_day_of_the_week = new Date(date_today.setDate(date_today.getDate() 
                              - date_today.getDay() + 1));


let last_day_of_the_week = new Date(date_today.setDate(date_today.getDate() 
                              - date_today.getDay() + 7));


console.log(`The first date of the week is: ${first_day_of_the_week}`);


console.log(`The last day of the week is: ${last_day_of_the_week}`);

Output

The current day index of the week is: 1

The current date of the week is: 26

The first date of the week is: Mon Sep 26 2022 12:17:42 GMT+0530 (India Standard Time)

The last day of the week is: Sun Oct 02 2022 12:17:42 GMT+0530 (India Standard Time)

That’s it!

Leave a Comment