How to Get the Day Name from a Date in JavaScript
A common requirement in UI development is to display the name of the day of the week (e.g., "Monday") instead of its number. While the Date.prototype.getDay() method returns a number (0 for Sunday, 1 for Monday, etc.), you need an extra step to get the name. The modern, standard way to do this is with the Intl (Internationalization) API, which provides a powerful, locale-aware solution.
This guide will teach you how to use the Intl.DateTimeFormat object to get the day's name in various formats. We will also cover the classic array lookup method and explain why the Intl API is the superior choice for most applications.
The Core Problem: getDay() Returns a Number
The built-in getDay() method does not return a string like "Monday." Instead, it returns a zero-based integer representing the day of the week.
Example:
const myDate = new Date('2025-10-27'); // This is a Monday
// Problem: getDay() returns a number, not the name "Monday".
const dayIndex = myDate.getDay();
console.log(dayIndex); // Output: 1
Our task is to convert this index into a human-readable name.
The Modern Method (Recommended): Intl.DateTimeFormat
The Intl.DateTimeFormat object is the standard, built-in tool for language-sensitive date and time formatting. It is the best choice because it can automatically produce the correct day name for any language or region.
Solution:
function getDayName(date, locale = 'en-US') {
const formatter = new Intl.DateTimeFormat(locale, { weekday: 'long' });
return formatter.format(date);
}
// Example Usage:
const myDate = new Date('2025-10-27'); // A Monday
console.log(getDayName(myDate)); // Output: "Monday"
// You can easily get the name in another language
console.log(getDayName(myDate, 'es-ES')); // Output: "lunes"
console.log(getDayName(myDate, 'de-DE')); // Output: "Montag"
A simpler but less flexible alternative is date.toLocaleDateString('en-US', { weekday: 'long' }), which uses the Intl API under the hood.
How to Get Different Name Formats (long, short, narrow)
The Intl.DateTimeFormat API is highly configurable. You can get different versions of the day name by changing the weekday option.
const date = new Date('2025-10-27'); // A Monday
// 'long' format
const longFormatter = new Intl.DateTimeFormat('en-US', { weekday: 'long' });
console.log(longFormatter.format(date)); // Output: "Monday"
// 'short' format
const shortFormatter = new Intl.DateTimeFormat('en-US', { weekday: 'short' });
console.log(shortFormatter.format(date)); // Output: "Mon"
// 'narrow' format
const narrowFormatter = new Intl.DateTimeFormat('en-US', { weekday: 'narrow' });
console.log(narrowFormatter.format(date)); // Output: "M"
The narrow format should be used with caution, as different days can have the same initial (e.g., Tuesday and Thursday).
The Classic Method: Manual Array Lookup
Before the Intl API was widely available, the standard way to get a day name was to create a manual lookup array.
function getDayName(date) {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const dayIndex = date.getDay();
return days[dayIndex];
}
// Example Usage:
const myDate = new Date('2025-10-27'); // A Monday
console.log(getDayName(myDate)); // Output: "Monday"
Drawback:
- This method is not locale-aware.
- It only works for a single, hardcoded language (in this case, English).
- If you need to support multiple languages, you would have to manage different arrays and translation logic yourself, which is complex and error-prone.
This is the primary reason the Intl.DateTimeFormat approach is the superior, professional choice.
Conclusion
For converting a Date object to a day name, modern JavaScript provides a powerful and flexible solution.
- The
Intl.DateTimeFormatAPI is the recommended best practice. It is the most powerful, flexible, and correct solution for any user-facing application, as it is fully locale-aware. Use the{ weekday: 'long' }option for the full name. - A manual array lookup based on the index from
.getDay()is a functional and simple alternative for applications that are guaranteed to only ever support a single language.