How to get the Day of a Week in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 24 20212 min read

banner

Photo by Sam Headland

Get the Day of a Week in JavaScript #

To get the day of a week, call the getDay() method on the Date object. 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 parameters 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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee