Home >Web Front-end >JS Tutorial >How to Get Month Names from Dates in JavaScript?
How to Extract Month Names from Dates
In JavaScript, you can effortlessly retrieve the name of a month from a Date object. Here's a comprehensive guide:
Using the toLocaleString() Method:
This method allows you to format date objects as strings according to locale-specific conventions. For month names, specify an object with the "month" key set to one of the following values:
Example:
var objDate = new Date("10/11/2009"); // Get the month name in long format const monthNameLong = objDate.toLocaleString('default', { month: 'long' }); // Get the month name in short format const monthNameShort = objDate.toLocaleString('default', { month: 'short' }); console.log(`Long month name: ${monthNameLong}`); console.log(`Short month name: ${monthNameShort}`);
This will output:
Long month name: October Short month name: Oct
The above is the detailed content of How to Get Month Names from Dates in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!