如何从月份编号中获得月份名称?

例如,如果我有3,我想返回march

date.tm_month()

如何获得弦乐进行曲?


当前回答

import datetime

monthinteger = 4

month = datetime.date(1900, monthinteger, 1).strftime('%B')

print month

4月

其他回答

import datetime

monthinteger = 4

month = datetime.date(1900, monthinteger, 1).strftime('%B')

print month

4月

日历火

从那里你可以看到那个日历。month_name[3]将返回March,数组索引为0是空字符串,因此也不需要担心零索引。

如果你只需要知道给定数字(1 - 12)的月份名称,这就不太有用了,因为当前日期并不重要。

calendar.month_name[我]

or

calendar.month_abbr[我]

在这里更有用。

这里有一个例子:

import calendar

for month_idx in range(1, 13):
    print (calendar.month_name[month_idx])
    print (calendar.month_abbr[month_idx])
    print ("")

样例输出:

January
Jan

February
Feb

March
Mar

...

对于任意范围的月份数字

month_integer=range(0,100)
map(lambda x: calendar.month_name[x%12+start],month_integer)

将产生正确的列表。从月份-整数列表中一月份开始的位置调整start-parameter。

一次打印所有月份:

 import datetime

 monthint = list(range(1,13))

 for X in monthint:
     month = datetime.date(1900, X , 1).strftime('%B')
     print(month)