Home >Web Front-end >JS Tutorial >How Can I Retrieve the Month Name from a JavaScript Date Object?

How Can I Retrieve the Month Name from a JavaScript Date Object?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-07 19:13:16589browse

How Can I Retrieve the Month Name from a JavaScript Date Object?

Retrieve Month Name from JavaScript Date

In JavaScript, you can access essential date information, including the month. This guide demonstrates how to obtain the month name from a given date object.

For instance, given the date object:

var objDate = new Date("10/11/2009");

Solution Using ECMAScript Internationalization API:

The ECMAScript Internationalization API provides a standardized way to format dates according to different locales. You can use it to retrieve the month name as follows:

const date = new Date(2009, 10, 10);  // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
console.log(month);

This will output "November" since the 'long' option formats the month in its full name. You can also use the 'short' option to get a shorter name, such as "Nov."

Additional Notes:

  • The 'default' locale is used to format the date according to the browser's settings.
  • You can specify a specific locale by replacing 'default' with a language code, such as 'en-US'.
  • The month name may vary depending on the locale.

The above is the detailed content of How Can I Retrieve the Month Name from a JavaScript Date Object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn