How to get the Day of the Week in JavaScript

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

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

In the first example, we used the 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

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, 03, 24); // ๐Ÿ‘‡๏ธ Saturday console.log( date.toLocaleDateString('en-US', { weekday: 'long', }), ); // ๐Ÿ‘‡๏ธ Samstag console.log( date.toLocaleDateString('de-DE', { weekday: 'long', }), );

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, 03, 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', }), );

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

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 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 ยฉ 2023 Borislav Hadzhiev