Sometimes, we want to convert month name to month number in JavaScript.
In this article, we’ll look at how to convert month name to month number in JavaScript.
How to convert month name to month number in JavaScript?
To convert month name to month number in JavaScript, we use the date toLocaleDateString method.
For instance, we write
const month = new Date().toLocaleDateString(`en`, { month: `2-digit` });
to call toLocaleDateString with the locale string and an object with the month property.
We set it to '2-digit' to return a 2 digit month.
Conclusion
To convert month name to month number in JavaScript, we use the date toLocaleDateString method.