Blog
Dates - How to convert a month name (eg January, Feburary, March) to month number (eg, 1, 2, 3)
January 22. 2024
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.
- Categories:
- dates
Related posts
- Dates - How to format date day numbers with suffix (st nd rd th)
- Formula - How to get the dates for the current week
- Dates - How to calculate UK tax weeks
- Dates - How to convert dates from MM DD YYYY to DD MM YYYY and vice versa
- Dates - How to detect overlapping times / scheduling conflicts
- Dates - How to display or convert Excel date serial numbers
- Dates - How to get the start date from week number
- Dates - How to check if a year is a leap year
- Dates - How to get the week number for a given date
- Dates - How to get the nth weekday for a month
- Dates - How to get the last weekday for a month
- Dates - How to get a numbered list of days or months, starting from a specified day or month
- Controls - How to display dates in a combo box
- Search - How to filter records by month and year using a delegable expression