How can I get the month name from the month number?
For instance, if I have 3
, I want to return march
date.tm_month()
How to get the string march
?
How can I get the month name from the month number?
For instance, if I have 3
, I want to return march
date.tm_month()
How to get the string march
?
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:
Sample output:
Calendar API
From that you can see that
calendar.month_name[3]
would returnMarch
, and the array index of0
is the empty string, so there's no need to worry about zero-indexing either.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.For the example above, it would return
Dec
.For arbitaray range of month numbers
will yield correct list. Adjust
start
-parameter from where January begins in the month-integer list.I created my own function converting numbers to their corresponding month.
Then I can call the function. For example:
Outputs: