0%

如何使用Python生成一个日期列表

功能描述:只要输入起始日期和终止日期就能生成在这段时间内的一个日期列表,日期格式为:19940214

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import datetime

def datelist(start, end):
start_date = datetime.date(*start)
end_date = datetime.date(*end)

result = []
curr_date = start_date
while curr_date != end_date:
result.append("%04d%02d%02d" % (curr_date.year, curr_date.month, curr_date.day))
#datetime.timedelta返回两个时间的差值
curr_date += datetime.timedelta(days=1)
result.append("%04d%02d%02d" % (curr_date.year, curr_date.month, curr_date.day))
return result

if __name__ == "__main__":
start_date = input("The start_date(year-month-day):")
end_date = input("The end_date(year-month-day):")
(start_date_year,start_date_month,start_date_day) = start_date.split('-')
(end_date_year,end_date_month,end_date_day) = end_date.split('-')
print(datelist((int(start_date_year),int(start_date_month),int(start_date_day)),(int(end_date_year),int(end_date_month),int(end_date_day))))

参考链接:https://www.zhihu.com/question/35455996


- - - - - - - - - 本文结束啦感谢您阅读 - - - - - - - - -