Skip to content
Advertisement

Get month number from month name

I have this.

JavaScript

How to get month number which contain in monthName variable? Thanks!

Advertisement

Answer

Use Java’s Calendar class. It can parse any given string into a valid calendar instance. Here is an example (assuming that the month is in english).

JavaScript

You can specify the language in SimpleDateFormat:

JavaScript

By default, Java uses the user’s local to parse the string.

Keep in mind that a computer starts counting at 0. So, January will be 0. If you want a human readable date, you should format the calendar instance:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement