How to Get the Day of the Week in JavaScript
When working with dates, you often need to know the day of the week. This can be for scheduling, displaying calendar information, or implementing conditional logic (e.g., "disable weekends"). JavaScript's Date object provides two primary ways to get this information: as a numeric index or as a formatted, human-readable name.
This guide will teach you the modern, standard methods for getting both the numeric day of the week and its localized name.
Goal 1: Get the Day of the Week as a Number (0-6)
If you need the day of the week for mathematical or logical operations, the Date.prototype.getDay() method is the tool for the job.
The getDay() method returns an integer between 0 and 6 that corresponds to the day of the week. It follows a standard where the week begins on Sunday.
- 0 = Sunday
- 1 = Monday
- 2 = Tuesday
- ...
- 6 = Saturday
For example, we want to get a numeric representation of the day of the week for a given date.
// Problem: What is the numeric day of the week for today?
const today = new Date();
Solution:
const today = new Date(); // Let's assume today is a Saturday
const dayIndex = today.getDay();
console.log(dayIndex); // Output: 6
Important: Do not confuse this with getDate(), which returns the day of the month (1-31).
Goal 2: Get the Name of the Day of the Week (Recommended)
For any user-facing application, displaying the name of the day (e.g., "Sunday") is much more useful than a number. The modern, standard, and most flexible way to do this is with the Intl (Internationalization) API, which is accessible via the toLocaleString() method.
The toLocaleString() method can take a locale and an options object. By specifying { weekday: '...' } in the options, you can extract just the name of the day, correctly formatted for any language.
For example, we want to display the full name of the day of the week for a given date.
Solution:
function getDayName(date, locale = 'en-US') {
return date.toLocaleDateString(locale, { weekday: 'long' });
}
// Example Usage:
const myDate = new Date('2025-10-7'); // This was a Friday
console.log(getDayName(myDate)); // Output: Friday
How to Customize the Format
The weekday option has three possible values, allowing you to control the length of the day's name.
const myDate = new Date('2025-10-17');
// 'long' (the full name)
console.log(myDate.toLocaleDateString('en-US', { weekday: 'long' })); // Output: Friday
// 'short' (typically three letters)
console.log(myDate.toLocaleDateString('en-US', { weekday: 'short' })); // Output: Fri
// 'narrow' (typically one letter, can be ambiguous)
console.log(myDate.toLocaleDateString('en-US', { weekday: 'narrow' })); // Output: F
How to Get the Name in a Different Language
To get the name in another language, simply change the locale.
const myDate = new Date('2025-10-17');
console.log(myDate.toLocaleDateString('es-ES', { weekday: 'long' })); // Output: viernes
console.log(myDate.toLocaleDateString('de-DE', { weekday: 'long' })); // Output: Freitag
Conclusion
Getting the day of the week in JavaScript is a simple task if you choose the right method for your goal.
- To get the day as a numeric index (0-6) for logical operations, use the
date.getDay()method. - To get the name of the day for display purposes, the
date.toLocaleString(locale, { weekday: '...' })method is the modern and recommended best practice. It is powerful, flexible, and handles internationalization correctly.