Home > Web Front-end > JS Tutorial > How to Retrieve a Month Name from a Date in JavaScript?

How to Retrieve a Month Name from a Date in JavaScript?

DDD
Release: 2024-12-04 09:34:17
Original
913 people have browsed it

How to Retrieve a Month Name from a Date in JavaScript?

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]);
Copy after login

Using the Internationalization API:

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

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template