How to convert month index to name in python

Published: January 23, 2019

DMCA.com Protection Status

To convert month index (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ou 12) to name (January, February, March, April, May, June, July, August, September, October, November, December), the simplest way is to use python module calendar:

>>> import calendar
>>> month_idx = 9
>>> month_name = calendar.month_name[month_idx]
>>> month_name
'September'

another example:

>>> for month_idx in range(1,13):
...     calendar.month_name[month_idx]
... 
'January'
'February'
'March'
'April'
'May'
'June'
'July'
'August'
'September'
'October'
'November'
'December'

To change language use locale, in French for instance:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'fr_FR')
'fr_FR'
>>> for month_idx in range(1,13):
...     calendar.month_name[month_idx]
... 
'janvier'
'février'
'mars'
'avril'
'mai'
'juin'
'juillet'
'août'
'septembre'
'octobre'
'novembre'
'décembre'

References

Links Site
Get month name from number stackoverflow
calendar docs.python.org
Mois wikipedia
Python calendar: day/month names in specific locale stackoverflow
locale python doc