假如要获取最近六个月的每个月的年月和对应每个月的天数:
import calendar
from datetime import datetime
# 获取年、月
this_year = datetime.now().year
this_month = datetime.now().month
date_list = [[this_year,this_month,calendar.monthlen(this_year, this_month)]]
for i in range(5):
year_month_days = list(calendar.prevmonth(this_year, this_month))
this_month = this_month - 1
if this_month <= 0:
this_month = 12
this_year = this_year -1
year_month_days.append(calendar.monthlen(this_year, this_month))
date_list.append(year_month_days)
for date_tuple in date_list:
if date_tuple[1] < 10:
date_tuple[1] = "0" + str(date_tuple[1])
print(date_list)
以上代码根据自己需求修改就可以 。。。
QQ交流群:121160124