It is now possible to do this with the ECMAScript Internationalization API:
const date = new Date(2009, 10, 10); // 2009-11-10_x000D_
const month = date.toLocaleString('default', { month: 'long' });_x000D_
console.log(month);
_x000D_
'long'
uses the full name of the month, 'short'
for the short name, and 'narrow'
for a more minimal version, such as the first letter in alphabetical languages.
You can change the locale from browser's 'default'
to any that you please (e.g. 'en-us'
), and it will use the right name for that language/country.
With toLocaleString
api you have to pass in the locale and options each time. If you are going do use the same locale info and formatting options on multiple different dates, you can use Intl.DateTimeFormat
instead:
const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });_x000D_
const month1 = formatter.format(new Date());_x000D_
const month2 = formatter.format(new Date(2003, 5, 12));_x000D_
console.log(`${month1} and ${month2}`); // current month in French and "juin".
_x000D_
For more information see my blog post on the Internationalization API.