Retrieve Month Name from Date in JavaScript
JavaScript provides various methods to manipulate and retrieve information from date objects. One common task is obtaining the name of the month associated with a specific date. This article will explore how to achieve this functionality using the built-in Date object and the ECMAScript Internationalization API.
Using the Date Object:
var objDate = new Date("10/11/2009"); // Extract the month number (0-based) var monthNum = objDate.getMonth(); // Use an array of month names to retrieve the full name var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; // Display the month name console.log(monthNames[monthNum]);
Using the Internationalization API:
const date = new Date(2009, 10, 10); // 2009-11-10 const month = date.toLocaleString('default', { month: 'long' }); console.log(month);
This method is more sophisticated and allows for localization and formatting options based on the user's locale.
The above is the detailed content of How to Retrieve a Month Name from a Date in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!