How to get the day of the week in JavaScript
Determining the day of the week from a date is essential for scheduling applications, calendar components, and business logic that depends on weekdays.
As the creator of CoreUI, a widely used open-source UI library, I’ve built numerous calendar and scheduling components that require precise weekday calculations.
From my expertise, the most reliable solution is to use the getDay()
method, which returns a number from 0-6 representing the day of the week.
This method is consistent across all browsers and provides the foundation for both numeric and text-based weekday representations.
Use the getDay()
method to get the day of the week as a number (0-6).
const day = new Date().getDay()
The getDay()
method returns an integer from 0 to 6, where 0 represents Sunday, 1 is Monday, 2 is Tuesday, and so on through 6 for Saturday. This numeric representation is perfect for conditional logic and calculations. To get the day name instead, you can use an array lookup like ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][day]
or use toLocaleDateString('en', {weekday: 'long'})
for localized day names.
Best Practice Note:
This is the same approach we use in CoreUI calendar components for scheduling and date picker functionality.
Remember that getDay()
starts with Sunday as 0, which differs from some other programming languages that start with Monday as 1.