How to get the Day of the Week in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Get the Day of the Week in JavaScript #

Call 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 specific 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.

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

We passed the following arguments to the 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, pass the locale as the first parameter to the 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', }), );

Setting the day to long gives us the entire name of the day. The short value gives us the first 3 letters of the day, and narrow - just the first letter.

Further Reading #

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.