Home > Web Front-end > JS Tutorial > How Can I Extract Month Names from Dates in JavaScript?

How Can I Extract Month Names from Dates in JavaScript?

Mary-Kate Olsen
Release: 2024-12-07 03:05:12
Original
448 people have browsed it

How Can I Extract Month Names from Dates in JavaScript?

Retrieving Month Names from Dates in JavaScript

When working with dates, it's often necessary to extract the month name, such as "October" or "Oct." A straightforward approach exists using the ECMAScript Internationalization API.

For the example date object:

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

To generate the month name, utilize the following code:

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

This code snippet leverages the toLocaleString() method with the month option set to 'long' to retrieve the month name in full (e.g., "October"). The 'default' locale setting conforms to the browser's current locale. The resulting month name is assigned to the month variable and logged to the console, outputting "October" for the given example date.

The above is the detailed content of How Can I Extract Month Names from Dates in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template