How to get the Day of the Week in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Get the Day of the Week in JavaScript

Use the getDay() method on the Date object to get the day of the week.

The getDay method returns the day of the week for the specified date, represented as an integer between 0 and 6, where 0 is Sunday and 6 is Saturday.

index.js
const dayOfWeekDigit = new Date().getDay(); console.log(dayOfWeekDigit); // ๐Ÿ‘‰๏ธ 0 const dayOfWeekName = new Date().toLocaleString('default', { weekday: 'long', }); console.log(dayOfWeekName); // ๐Ÿ‘‰๏ธ Sunday

get day of week in javascript

The code for this article is available on GitHub

In the first example, we used the Date.getDay() method to get an integer from 0 to 6 that represents the day of the week for the supplied date.

index.js
const dayOfWeekDigit = new Date().getDay(); console.log(dayOfWeekDigit); // ๐Ÿ‘‰๏ธ 0

Our second example uses the toLocaleString() method to get the name of the day of the week.

index.js
const dayOfWeekName = new Date().toLocaleString( 'default', {weekday: 'long'} ); console.log(dayOfWeekName); // ๐Ÿ‘‰๏ธ Sunday
The code for this article is available on GitHub

We passed the following arguments to the toLocaleString() method:

  1. the locale - in which language the name of the day should be returned. By specifying default, it can vary based on the user's browser preferences.
  2. the options object - we set the weekday setting to long to get the full name of the day. Other possible values are short and narrow.

If you want to get the day's name in a different locale, specify the locale in the call to the toLocaleDateString() method.

index.js
const date = new Date(2027, 3, 24); // ๐Ÿ‘‡๏ธ Saturday console.log( date.toLocaleDateString('en-US', { weekday: 'long', }), ); // ๐Ÿ‘‡๏ธ Samstag console.log( date.toLocaleDateString('de-DE', { weekday: 'long', }), );

getting day name in different locale

The code for this article is available on GitHub

If you need to get the day's name in a different format, e.g. the first 3 letters or just the first letter, update the value of the weekday property in the options object.

index.js
const date = new Date(2027, 3, 24); // ๐Ÿ‘‡๏ธ Saturday console.log( date.toLocaleDateString('en-US', { weekday: 'long', }), ); // ๐Ÿ‘‡๏ธ Sat console.log( date.toLocaleDateString('en-US', { weekday: 'short', }), ); // ๐Ÿ‘‡๏ธ S console.log( date.toLocaleDateString('en-US', { weekday: 'narrow', }), );

getting day name in different format

The code for this article is available on GitHub

When the weekday is set to long, the entire name of the day is returned.

When the weekday is set to short, only the first 3 letters of the day are returned.

The narrow value returns only the first letter of the day.

Note that there are weekdays that have the same first letter in many different locales, e.g. Saturday and Sunday in English.

If you have to do this often, define reusable functions.

index.js
function getDayOfWeek(date = new Date()) { return date.getDay(); } const dayOfWeek = getDayOfWeek(); console.log(dayOfWeek); // ๐Ÿ‘‰๏ธ 3 // -------------------------------------------- function getDayOfWeekName(date = new Date()) { return date.toLocaleDateString('default', { weekday: 'long', }); } const dayOfWeekName = getDayOfWeekName(); console.log(dayOfWeekName); // ๐Ÿ‘‰๏ธ Wednesday

defining reusable function

The code for this article is available on GitHub

The getDayOfWeek function returns the day of the week for the specified date, represented as an integer between 0 and 6, where 0 is Sunday and 6 is Saturday.

The getDayOfWeekName function returns the name of the day of the week.

Both functions take an option Date parameter that defaults to the current date.

I've also written a detailed article on how to get the day name from a specific Date.

# 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