DEV Community

Cover image for The best way to get the month name from a date in JavaScript

The best way to get the month name from a date in JavaScript

Tapas Adhikary on September 18, 2023

JavaScript Date object provides many helpful methods for dealing with date and time. The list is so vast that it is sometimes hard for developers t...
Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

Another approach could be to use Intl.DateTimeFormat.

Collapse
 
atapas profile image
Tapas Adhikary

The toLocaleString() method internally call the Intl’s dateFormat method.

Here is a small read about it from MDN

“ The toLocaleString() method of Date instances returns a string with a language-sensitive representation of this date. In implementations with Intl.DateTimeFormat API support, this method simply calls Intl.DateTimeFormat.”

Hence I guess it already fits the approach you suggested 😀

Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

@atapas Agreed. I was referring to Intl.DateTimeFormat because it may trigger curiosity in the reader. Intl refers to internationalisation, writing for those that don't know what it is.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Came for this, specially when the title says "THE BEST" 😅
intl sure is the way to go whenever you want to fully support internationalisation and to cover all edgy cases I can think of.

Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

@joelbonetr

Came for this, specially when the title says "THE BEST"

Oh, everyone can miss anything. Hence, comments are a great way to contribute to great posts. :-)

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

Sure! My bad, honestly it's hard to me nowadays to differentiate between click bait and a honest miss 😅

Thread Thread
 
atapas profile image
Tapas Adhikary

Of course! Comments are a great way to discuss and get better clarifications. Great to have this discussion here.

Getting a bit deeper, the toLocaleString(the best approach in this article) still uses the Intl’s date format which is already locale-aware. So, the intention was never to sound it like a clickbait one, but rather provide a solution that is far better than other older alternatives mentioned. I think, adding a few lines about the Intl will be great to avoid the confusion. What say?

Thread Thread
 
cezarytomczyk profile image
Cezary Tomczyk

@atapas
I think, adding a few lines about the Intl will be great to avoid the confusion. What say?

I'd say yes, please. :-)

Thread Thread
 
atapas profile image
Tapas Adhikary

Done!

Thanks.

Thread Thread
 
cezarytomczyk profile image
Cezary Tomczyk

@atapas One small thing. Link "here" makes no sense out of the context (accessibility rule). I'd propose:

Intl.DateTimeFormat API for enabling locale-aware date-time formatting. You can read more about Intl.DateTimeFormat on MDN site.

where Intl.DateTimeFormat on MDN site becomes a link.

More about link purpose.

Thread Thread
 
atapas profile image
Tapas Adhikary

Great suggestion, done 🫡

Collapse
 
mayurkode profile image
Mayur Kode

Thanks for the article!
useful

Collapse
 
rickdelpo1 profile image
Rick Delpo

I like the array of months method. A while back I wrote a use case for using dates in Javascript without the Moment library. In this example we populate a stacked bar chart for each month (with plain vanilla js and css)
click here, then get the fully commented source code from my codepen
dev.to/rickdelpo1/how-to-populate-...

Happy coding !!