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");
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);
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!