If you have a month number and want to get the month name for that number, then go through the following steps:
Date()
constructor and do not pass any arguments to it.setMonth()
method and pass the month number.'en-US'
as locales
parameter value. If you want the full name of the month, then pass month: 'long'
as a key-value pair to the options parameter.
date.toLocaleString('en-US', { month: 'long' });On the other hand, if you want the short name of the month, then pass
month: 'short'
as a key-value pair.
date.toLocaleString('en-US', { month: 'short' });
Suppose you have 1 as a month number and want to get the month name for it, then run the following code:
let date = new Date(); date.setMonth(1 - 1); const monthLong = date.toLocaleString('en-US', { month: 'long' }); console.log(monthLong); //"January" const monthShort = date.toLocaleString('en-US', { month: 'short' }); console.log(monthShort); //"Jan"
Similarly, you can get the month name for month number 9.
let date = new Date(); date.setMonth(9 - 1); const monthLong = date.toLocaleString('en-US', { month: 'long' }); console.log(monthLong); //"September" const monthShort = date.toLocaleString('en-US', { month: 'short' }); console.log(monthShort); //"Sep"
Here, 1 is subtracted from the month number because the Date class follows zero-based numbering for months.