Read on to know how to get the Sunday of the current week using JavaScript. Using the instructions of this tutorial, you can also calculate any date of the week.
Get The Sunday Of The Current Week Using JavaScript
Our program is built around the getDay() method, which returns the day of the week for the date and time represented in Date objects. It has a simple syntax with no parameters:
getDay()
This method returns an integer between 0 and 6, indicating the day of the week for the Date object. Keep in mind that this method uses your system’s local time to interpret the date and time. Sunday is represented by the number 1, Monday by 1, Tuesday by 2, and so on.
For instance, you can check the day of the week of the 2020 United States presidential election (November 3):
const election = new Date('November 3, 2020');
console.log(election.getDay());
Output:
2
The returned value (2) indicates that the election took place on a Tuesday, in accordance with American laws. Similarly, we can use this method to find out the day of the week of today, from which we can calculate the date of the week’s Sunday.
You can get a Date object representing the date and time at the moment with the Date() constructor. Normally, you can give it any date and time value. When these components are omitted, and no parameters are provided, the Date() constructor will create a Date object representing the date and time of the moment it is invoked.
let date = new Date();
Suppose where you live, Sunday is the first day of the week. The returned value of date.getDay() is the number of days between Sunday and today:
const today_dotw = date.getDay();
console.log(today_dotw);
Output:
3
Using this number, we can create a Date object representing the Sunday of the current week with getDate() and setDate().
Those methods deal with the day of the month of a given Date object. For instance, let’s invoke getDate() on the Date object representing today:
const today_dotm = date.getDate();
console.log(today_dotm);
Output:
1
It shows that we are currently on the first day of the month. Meanwhile, the setDate() can change a Date object to any date of the month we want. To set it to the Sunday of the week, we can subtract it by three days:
date.setDate(today_dotm - today_dotw);
console.log("Sunday: ", date.toLocaleDateString('en-CA'));
Output:
Sunday: 2022-08-28
We use the Canadian English language code to display dates in the YYYY-MM-DD format. Check out this guide if you want to convert a date to the ISO format.
Here is the full code illustrating how you can find the Sunday of the current week with JavaScript:
let date = new Date();
console.log("Today:", date.toLocaleDateString('en-CA'))
const today_dotw = date.getDay();
const today_dotm = date.getDate();
date.setDate(today_dotm - today_dotw);
console.log("Sunday: ", date.toLocaleDateString('en-CA'));
Output:
Today: 2022-09-01
Sunday: 2022-08-28
Remember to only use the code above if you consider Sunday the first day of the week. When this isn’t the case, the calculation needs some modifications.
For instance, if your culture’s convention recognizes Monday as the first day of the week (which is also compatible with ISO date standards), you need to offset it by a full week. Your code now should look like this:
date.setDate(today_dotm - today_dotw + 7);
console.log("Sunday: ", date.toLocaleDateString('en-CA'));
Output:
Today: 2022-09-01
Sunday: 2022-09-04
Summary
To get the Sunday of the current week using JavaScript, you can use a combination of built-in Date methods. Remember to adjust the algorithm to the numbering convention of your location.
Leave a comment