例如我想用现在的时间,每次循环减去一个月,获得一个这个数据,直到2019年的5月份、
2022-01
2021-12
2021-11
..。..
2019-05
import datetime
import dateutil.relativedelta
t = datetime.datetime.now()
while t.strftime("%Y-%m")>'2019-05':
t = t - dateutil.relativedelta.relativedelta(months=1)
print(t.strftime("%Y-%m"))
for year in range(2022,2018,-1):
for month in range(12,0,-1):
if year==2022:
temp="01"
print("{}-{}".format(year,temp))
break
else:
temp=""
if month<10:
temp="0"+str(month)
else:
temp=str(month)
print("{}-{}".format(year, temp))