How to get the name of the day from a date in JavaScript?

May 18, 2024

There are multiple ways to get the name of the day in JavaScript. In this guide, we will learn two most simple ways to get the name of day of the week in JavaScript.

Using toLocaleDateString()

We’ll use the built-in toLocaleDateString() method from the date object, its the most straightforward solution.

Date.toLocaleDateString() takes two parameters:

  1. locales (string)
  2. options (object)

For the first parameter (locales) we will just say "default", and for the second parameter (options), we will give an object with a key named weekday and for the value, we have two choices, we can say long or short.

long gives us the full name of the day, e.g. Monday or Saturday.

short gives us a three letter name of the day, e.g. Mon or Sat.

const today = new Date();

// Full name (Sunady or Monday)
const a = today.toLocaleDateString("default", { weekday: "long" });

// Short version (Sun or Mon)
const b = today.toLocaleDateString("default", { weekday: "short" });

Using getDate()

An alternative way to get the name of the day is by using the getDay() method from a date object.

First we will create an array of all the names of the days of the week, I’m guessing you already have a date to work with, for demonstration I’m using the current date (new Date()) as a date object.

The getDay() method returns the day of the week (0 to 6). So we can use the return value of getDay() as an index for the array to extract the name of the day.

const days = [
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
];
const today = new Date();

const dayName = days[today.getDay()];

So here you go, you’ve learned two ways to get the name of the day from a date object in JavaScript.