month name to month number and vice versa in python

Python

Python Problem Overview


I am trying to create a function that can convert a month number to an abbreviated month name or an abbreviated month name to a month number. I thought this might be a common question but I could not find it online.

I was thinking about the calendar module. I see that to convert from month number to abbreviated month name you can just do calendar.month_abbr[num]. I do not see a way to go the other direction though. Would creating a dictionary for converting the other direction be the best way to handle this? Or is there a better way to go from month name to month number and vice versa?

Python Solutions


Solution 1 - Python

Create a reverse dictionary using the calendar module (which, like any module, you will need to import):

{month: index for index, month in enumerate(calendar.month_abbr) if month}

In Python versions before 2.7, due to dict comprehension syntax not being supported in the language, you would have to do

dict((month, index) for index, month in enumerate(calendar.month_abbr) if month)

Solution 2 - Python

Just for fun:

from time import strptime

strptime('Feb','%b').tm_mon

Solution 3 - Python

Using calendar module:

Number-to-Abbr calendar.month_abbr[month_number]

Abbr-to-Number list(calendar.month_abbr).index(month_abbr)

Solution 4 - Python

Here's yet another way to do it.

def monthToNum(shortMonth):
    return {
            'jan': 1,
            'feb': 2,
            'mar': 3,
            'apr': 4,
            'may': 5,
            'jun': 6,
            'jul': 7,
            'aug': 8,
            'sep': 9, 
            'oct': 10,
            'nov': 11,
            'dec': 12
    }[shortMonth]

Solution 5 - Python

Information source: Python Docs

To get month number from month name use datetime module

import datetime
month_number = datetime.datetime.strptime(month_name, '%b').month

# To  get month name
In [2]: datetime.datetime.strftime(datetime.datetime.now(), '%a %b %d, %Y')
Out [2]: 'Thu Aug 10, 2017'

# To get just the month name, %b gives abbrevated form, %B gives full month name
# %b => Jan
# %B => January
dateteime.datetime.strftime(datetime_object, '%b')

Solution 6 - Python

Here is a more comprehensive method that can also accept full month names

def month_string_to_number(string):
    m = {
        'jan': 1,
        'feb': 2,
        'mar': 3,
        'apr':4,
         'may':5,
         'jun':6,
         'jul':7,
         'aug':8,
         'sep':9,
         'oct':10,
         'nov':11,
         'dec':12
        }
    s = string.strip()[:3].lower()

    try:
        out = m[s]
        return out
    except:
        raise ValueError('Not a month')

example:

>>> month_string_to_number("October")
10 
>>> month_string_to_number("oct")
10

Solution 7 - Python

One more:

def month_converter(month):
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    return months.index(month) + 1

Solution 8 - Python

Full month name to month number (e.g. January, February, etc..):

import datetime

month_name = 'January'
month_num = datetime.datetime.strptime(month_name, '%B').month

print(month_num, type(month_num))

>> 1 <class 'int'>

Partial month name to month number (e.g. Jan, Feb, etc..):

import datetime

month_name = 'Feb'
month_num = datetime.datetime.strptime(month_name, '%b').month

print(month_num, type(month_num))

>> 2 <class 'int'>

You can also format it to double-digits representation:

month_num = 3
formatted = f"{month_num:02}"

print(formatted, type(formatted))

>> 03 <class 'str'>

Month number to full month name (either double-digits representation or not, either string or int) (e.g. '01', 1, etc..):

import datetime

month_num = '04'  # month_num = 4 will work too
month_name = datetime.datetime(1, int(month_num), 1).strftime("%B")

print(month_name)

>> April

Month number to partial month name (either double-digits representation or not, either string or int) (e.g. '01', 1, etc..):

import datetime

month_num = 5  # month_num = '05' will work too
month_name = datetime.datetime(1, int(month_num), 1).strftime("%b")

print(month_name)

>> May

Solution 9 - Python

To get the full calendar name from the month number, you can use calendar.month_name. Please see the documentation for more details: https://docs.python.org/2/library/calendar.html

month_no = 1
month = calendar.month_name[month_no]

# month provides "January":
print(month)


Solution 10 - Python

Building on ideas expressed above, This is effective for changing a month name to its appropriate month number:

from time import strptime
monthWord = 'september'

newWord = monthWord [0].upper() + monthWord [1:3].lower() 
# converted to "Sep"

print(strptime(newWord,'%b').tm_mon) 
# "Sep" converted to "9" by strptime

Solution 11 - Python

form month name to number
d=['JAN','FEB','MAR','April','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
N=input()
for i in range(len(d)):
    if d[i] == N:
        month=(i+1)
print(month)

Solution 12 - Python

You can use below as an alternative.

  1. Month to month number:

from time import strptime

strptime('Feb','%b').tm_mon

  1. Month number to month:

import calendar

calendar.month_abbr[2] or calendar.month[2]

Solution 13 - Python

def month_num2abbr(month):
    month = int(month)
    import calendar
    months_abbr = {month: index for index, month in enumerate(calendar.month_abbr) if month}
    for abbr, month_num in months_abbr.items():
        if month_num==month:
            return abbr
    return False

print(month_num2abbr(7))

Solution 14 - Python

If you don't want to import the calendar library, and need something that is a bit more robust -- you can make your code a little bit more dynamic to inconsistent text input than some of the other solutions provided. You can:

  1. Create a month_to_number dictionary
  2. loop through the .items() of that dictionary and check if the lowercase of a string s is in a lowercase key k.

month_to_number = {
'January' : 1,         
'February' : 2,         
'March' : 3,           
'April' : 4,              
'May' : 5, 
'June' : 6,
'July' : 7, 
'August' : 8, 
'September' : 9, 
'October' : 10, 
'November' : 11, 
'December' : 12}

s = 'jun'
[v for k, v in month_to_number.items() if s.lower() in k.lower()][0]

Out[1]: 6

Likewise, if you have a list l instead of a string, you can add another for to loop through the list. The list I have created has inconsistent values, but the output is still what would be desired for the correct month number:

l = ['January', 'february', 'mar', 'Apr', 'MAY', 'JUne', 'july']
[v for k, v in month_to_number.items() for m in l if m.lower() in k.lower()]

Out[2]: [1, 2, 3, 4, 5, 6, 7]

The use case for me here is that I am using Selenium to scrape data from a website by automatically selecting a dropdown value based off of some conditions. Anyway, this requires me relying on some data that I believe our vendor is manually entering to title each month, and I don't want to come back to my code if they format something slightly differently than they have done historically.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark_MasoulView Question on Stackoverflow
Solution 1 - PythonDavid ZView Answer on Stackoverflow
Solution 2 - PythonMark BaileyView Answer on Stackoverflow
Solution 3 - PythondiogobernardinoView Answer on Stackoverflow
Solution 4 - PythonGi0rgi0sView Answer on Stackoverflow
Solution 5 - PythontheBuzzyCoderView Answer on Stackoverflow
Solution 6 - PythonharryhView Answer on Stackoverflow
Solution 7 - PythonAleksandr AleksandrovView Answer on Stackoverflow
Solution 8 - PythonElyasaf755View Answer on Stackoverflow
Solution 9 - PythonSian ThompsonView Answer on Stackoverflow
Solution 10 - PythonthescoopView Answer on Stackoverflow
Solution 11 - Pythonuser12643768View Answer on Stackoverflow
Solution 12 - PythonYogeshView Answer on Stackoverflow
Solution 13 - PythonKhaled KarmanView Answer on Stackoverflow
Solution 14 - PythonDavid EricksonView Answer on Stackoverflow