Get month name from number
This is not so helpful if you need to just know the month name for a given number (1 - 12), as the current day doesn't matter.
calendar.month_name[i]
or
calendar.month_abbr[i]
are more useful here.
Here is an example:
import calendar
for month_idx in range(1, 13):
print (calendar.month_name[month_idx])
print (calendar.month_abbr[month_idx])
print ("")
Sample output:
January
Jan
February
Feb
March
Mar
...
Calendar API
From that you can see that calendar.month_name[3]
would return March
, and the array index of 0
is the empty string, so there's no need to worry about zero-indexing either.
import datetime
mydate = datetime.datetime.now()
mydate.strftime("%B")
Returns: December
Some more info on the Python doc website
[EDIT : great comment from @GiriB] You can also use %b
which returns the short notation for month name.
mydate.strftime("%b")
For the example above, it would return Dec
.
import datetime
monthinteger = 4
month = datetime.date(1900, monthinteger, 1).strftime('%B')
print month
April