Skip to content

JavaScript get month name

  • by

In JavaScript, you can get the month name using the built-in Date object. The Date object has a method called toLocaleString() which can be used to retrieve the month name based on the locale of the user’s system or a specific locale you specify.

JavaScript gets month name example

Simple example code.

<!DOCTYPE html>
<html>
<body>
    <script>
        // Get the current date
        const d = new Date();

        // Get the month name using the user's locale
        const monthName = d.toLocaleString(navigator.language, { month: 'long' });

        console.log(monthName);
    </script>
</body>
</html>

Output:

JavaScript get month name

You can also use the toLocaleDateString() method with the { month: 'long' } option to achieve the same result.

// Get the current date
const currentDate = new Date();

// Get the month name using the user's locale
const monthName = currentDate.toLocaleDateString(navigator.language, { month: 'long' });

console.log(monthName);

Hard code

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

const d = new Date();
document.write("The current month is " + monthNames[d.getMonth()]);

Do comment if you have any doubts or suggestions on this Js date object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading