Blog

Dates - How to convert a month name (eg January, Feburary, March) to month number (eg, 1, 2, 3)

Do you need to convert a month name to the month number? If so, here's the formula to carry out this task.

When working with data, particuarly data that's been imported from other data sources, there can be a need to convert a month name (for example, January/February/March) to the month number (for example, 1/2/3).

How to convert a month name to a month number

The easiest way to convert a month name to number is to use the following structure.

Month(DateValue("1 " & "January" & " 2024"))

As the screenshot beneath illustrates, this formula converts the "Apr"  value in the text input control to 4.



The way that this formula works is to convert the string representation of the date (eg, 1-Apr-2024) to a date object by calling DateValue. The built-in Month function then returns the month number (eg, 4).

Note that this method converts dates in both short and long formats (eg, April and Apr will work).

If we enter an invalid month name, the formula will return an error.

How to convert a month name in non English languages to a month number

An important point to note here is that DateValue parses the string representation of the date depending on the language of the browser. If we want to convert month names in English and we have users that run Power Apps in languages other than English, it's important to specify the "en" language code like so.

Month(DateValue("1 " & "January" & " 2024", "en"))

By specifying a language code, we can convert month names in other languages to a month number.

For example, to convert Spanish month names to numbers, we would pass the "es" langauge code like so:

Month(DateValue("1 " & "enero" & " 2024", "es"))

As the screenshots beneath show, we can use this to convert month names in both short and long format.



Conclusion

If you need to convert month names to numbers, this post highlights a method that works with month names in short and long format, and also works with languages other than English.