Python3,使用生成器,生成一個列表,保存從歷史日期到現在所有的天
#!/usr/bin/python
import datetime
def date_print(day):
Lis = []
begin = datetime.datetime.strptime(day, '%Y-%m-%d')
#print(type(begin))
nowtime = datetime.datetime.today()
for i in range((nowtime - begin).days + 1):
yield i
date_day = begin + datetime.timedelta(days = i)
date = datetime.datetime.strftime(date_day, '%Y-%m-%d')
Lis.append(str(date))
print(Lis)
date1 = date_print("2019-11-29")
print(type(date1))
for i in date1:
pass
直接運行結果