Home > Web Front-end > JS Tutorial > How to Get Month Names from Dates in JavaScript?

How to Get Month Names from Dates in JavaScript?

Susan Sarandon
Release: 2024-12-03 16:06:18
Original
160 people have browsed it

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:

  • "short" - Abbreviated name (e.g., "Oct")
  • "long" - Full name (e.g., "October")

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

This will output:

Long month name: October
Short month name: Oct
Copy after login

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!

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