Get the Day Name from a specific Date using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
2 min

banner

# Get the Day Name from a specific Date using JavaScript

Use the toLocaleDateString() method to get the day name from a specific date.

The method returns a string with language sensitive representation of the date portion of the given date.

index.js
function getDayName(date = new Date(), locale = 'en-US') { return date.toLocaleDateString(locale, {weekday: 'long'}); } // โœ… Get name of current day console.log(getDayName()); // ๐Ÿ‘‰๏ธ Tuesday // โœ… Get day name for specific date console.log(getDayName(new Date('2022-01-29'))); // ๐Ÿ‘‰๏ธ Saturday // โœ… Get day name in different locale // ๐Ÿ‘‡๏ธ Samstag console.log(getDayName(new Date('2022-01-29'), 'de-DE'));

get day name from specific date

The code for this article is available on GitHub

We created a reusable function that takes a Date object and a locale as parameters and returns the name of the given date.

If no parameters are provided, the function returns the day name for the current date using the en-US locale.

We used the toLocaleDateString() method to get a string with language-sensitive representation of the current date.

The 2 parameters we passed to the method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options object, where we set the weekday property. Read more about the options object in the MDN docs.

The weekday property of the options object is the representation of the weekday.

We set it to long to get the full name of the weekday. The possible values include:

  • long - (e.g. Thursday)
  • short - (e.g. Thu)
  • narrow - (e.g. T). Two weekdays may have the same narrow style for some locales, e.g. Thursday and Tuesday are both T.

Note that the function we created has to be passed a Date() object as a parameter.

If you have a date string like 2022-04-29, you should pass the string to the Date() constructor to create a Date object when calling the function.

index.js
function getDayName(date = new Date(), locale = 'en-US') { return date.toLocaleDateString(locale, {weekday: 'long'}); } const dateStr = '2022-04-29'; // ๐Ÿ‘‡๏ธ "Friday" console.log(getDayName(new Date(dateStr))); // ๐Ÿ‘‡๏ธ "Freitag" console.log(getDayName(new Date(dateStr), 'de-DE'));

passing a string to the date constructor

The code for this article is available on GitHub

Passing a valid date or date and time string to the Date() constructor returns a Date object, on which we can call the toLocaleDateString method.

I've also written an article on how to convert month number to month name and vice versa.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev