How to get the month name from a JavaScript date
Given a JavaScript Date object instance, how can you get a string that represents the month name?
Given a JavaScript Date object instance, how can you get a string that represents the month name?
In other words, from
const today = new Date()
how can we get the month name?
Every Date object instance has a toLocaleString()
method, which is one of the JavaScript internationalization methods.
Using it you can get the month name in your current locale, and here’s how you can use it:
const today = new Date()
today.toLocaleString('default', { month: 'long' })
Depending on your current locale you’ll get a different result. I get “October” as a result.
Using the short
format for the date, I get “Oct”:
today.toLocaleString('default', { month: 'short' })
The first parameter, to which we pass the default
string, is the locale. You can pass any locale you want, for example it-IT
will return you ottobre
:
const today = new Date()
today.toLocaleString('it-IT', { month: 'long' })
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025