How to Convert a month number to name in JavaScript
This article will teach you how to use a JavaScript date object to change a month's number into a name.
The month name is returned in string format by the function below, which accepts the month number as an argument.
function getMonthName(month){ const d = new Date(); d.setMonth(month-1); const monthName = d.toLocaleString("default", {month: "long"}); return monthName; } getMonthName(11)
Output:
November
Some Explanations:
- The month name is obtained in string format using the function toLocaleString() method.
- Since the month number in JavaScript begins at 0, we subtracted -1 to convert it to JavaScript format.