How To Get The Day Name From A Specific Date Using JavaScript?

Get the Day Name from a specific Date using JavaScript

When building JavaScript applications, you may encounter requests to get date names. This article will show you how to get the day name from a specific date using JavaScript. Let’s go into detail now.

Get the day name from a specific date using JavaScript

This article uses JavaScript to provide two methods to get a date name from a specific date. They are:

  • Using the toLocaleDateString() method
  • Using the getDay() method

Using the toLocaleDateString() method

The toLocaleDateString() method returns the day and month names of a specific date object. Results are returned as strings and using language conventions.

Syntax:

toLocaleDateString(locales, options)

Parameters:

  • locales: A string or an array of strings whose language tag is BCP 47.
  • options: An object is adjusting the output format.

Example:

const date = new Date("2022-10-10");
console.log(date.toLocaleDateString("en-US", { weekday: "long" }));

Output:

Monday

In the above example, I have a date string of '2022-10-10'. First, I create a ‘Date’ object with the given date string. Then I call the toLocaleDateString() method on the newly created object.

The value of the first parameters passed to the toLocaleDateString() method is "en-US". This is the BCP 47 language tag of the US English language.

The second parameter passed in is the weekday property, which represents the names of the days of the week. We set the value of weekday to "long", which is the total name value of this property. In addition, this attribute can carry other values, such as:

  • "short": Short name of the day of the week.
  • "narrow": Contains only the first letter of the names of the days of the week.

To learn more about the parameters, click here.

Example:

const date = new Date("2022-10-10");
console.log(date.toLocaleDateString("en-US", { weekday: "long" }));
console.log(date.toLocaleDateString("en-US", { weekday: "short" }));
console.log(date.toLocaleDateString("en-US", { weekday: "narrow" }));

Output:

Monday
Mon
M

If we do not provide a specific date, the toLocaleDateString() method will return the date name of the current date in the default locale ‘en-US’.

And if we don’t provide any parameter, the toLocaleDateString() method will return the current date in the default format.

Example:

const date = new Date();
console.log(date.toLocaleDateString());
console.log(date.toLocaleDateString("en-US", { weekday: "long" }));

Output:

10/7/2022
Friday

Using the getDay() method

The getDay() method returns an integer (0 to 6) corresponding to the days of the week (from Sunday to Saturday).

Syntax:

getDay()

To get the date name from a specific date using the getDay() method, we will do the following steps:

  • Create an array containing the names of the days of the week from Sunday to Saturday.
  • Call the getDay() method on the date object created from the initial specific date.

Example:

const str = "2022-10-10";
const date = new Date(str);
const name = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const dateName = name[date.getDay()];
console.log(dateName);

Output:

Mon

Summary

This article has shown how to get the day name from a specific date using JavaScript. I hope the information in this article will be helpful to you. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *