How to Get the Day of the Week in JavaScript?

Get the day relative to a week in JavaScript

Get Day of the Week Using Date.prototype.getDay()

In JavaScript, you can get the day of the week (according to the local time) using the Date.prototype.getDay() method. It returns an integer between 0 and 6 (where 0 represents Sunday and 6 represents Saturday). For example, to get the day of the week for today's date, you could do something like the following:

const dt = new Date();
const day = dt.getDay();

console.log(day); // 0 - 6

Similarly, to get the day of the week for a specific date, you could pass in that date to the Date constructor like so:

const dt = new Date('March 27, 2021 00:50:00');
const day = dt.getDay();

console.log(day); // 6

Get the Textual Representation of the Day of the Week

In case, you need the name of the day instead of a numeric representation, you can use Intl.DateTimeFormat with one of the following options (passed in as the second argument to the constructor):

Option Description
{ weekday: 'long' } A full textual representation of the day of the week (Sunday through Saturday).
{ weekday: 'short' } A three-letter textual representation of a day (Mon through Sun).
{ weekday: 'narrow' } The initial letter of the day of the week — where two weekdays may have the same value depending on the locale, such as Tuesday and Thursday both being represented by "T".

For example:

const dt = new Date('March 27, 2021 00:50:00');

// output: Saturday
console.log(new Intl.DateTimeFormat('en-US', { weekday: 'long' }).format(dt));

// output: Sat
console.log(new Intl.DateTimeFormat('en-US', { weekday: 'short' }).format(dt));

// output: S
console.log(new Intl.DateTimeFormat('en-US', { weekday: 'narrow' }).format(dt));

Day of the Week Based on Locale

Using Intl.DateTimeFormat, internationalization is also possible. For example:

const dt = new Date('March 27, 2021 00:50:00');

// output: Samstag
console.log(new Intl.DateTimeFormat('de-DE', { weekday: 'long' }).format(dt));

Hope you found this post useful. It was published . Please show your love and support by sharing this post.