Using JavaScript Internationalization API we can now simply get month name from JavaScript Date object instance.
let today = new Date();
today.toLocaleString('default', { month: 'long' }); // April
This will return a full month name according to the current browser locale.
Month name can be represented in three styles: long
, short
and narrow
. Here are the remaining two:
today.toLocaleString('default', { month: 'short' }); // Apr
today.toLocaleString('default', { month: 'narrow' }); // A
We can also force any locale passing a string with BCP 47 language tag as a first parameter.
today.toLocaleString('pl-PL', { month: 'long' }); // kwiecień
This will return “kwiecień” which is Polish equivalent for “April”.
Useful links:
Can I use… Date: toLocaleString
Date.prototype.toLocaleString() | MDN
Intl.DateTimeFormat() constructor |MDN