Categories
JavaScript Answers

How to convert month name to month number in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *