Last updated: Mar 3, 2024
Reading timeยท3 min
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.
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
Date.getDay()
method to get an integer from 0
to 6
that represents the day of the week for
the supplied date.
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.
const dayOfWeekName = new Date().toLocaleString( 'default', {weekday: 'long'} ); console.log(dayOfWeekName); // ๐๏ธ Sunday
We passed the following arguments to the toLocaleString()
method:
default
, it can vary based on the user's browser preferences.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.
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', }), );
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.
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', }), );
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.
If you have to do this often, define reusable functions.
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've also written a detailed article on how to get the day name from a specific Date.
You can learn more about the related topics by checking out the following tutorials: