Categories
JavaScript Answers

How to get the day of week and the month of the year with JavaScript?

Spread the love

Sometimes, we want to get the day of week and the month of the year with JavaScript.

In this article, we’ll look at how to get the day of week and the month of the year with JavaScript.

How to get the day of week and the month of the year with JavaScript?

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.

For instance, we write

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false,
};
const prnDt = new Date().toLocaleTimeString("en-us", options);

to call toLocaleDateString on the Date object with the current date time with the locale string and options.

In options, we specify how to format different parts of the dates.

And it returns a date string with the formatted date and time.

'numeric' means the full number, '2-digit' formats the number as a 2 digit number.

'long' returns the full human readable date or time part.

Conclusion

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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