将两个时间点之间所有的天数输出到一个列表
import datetime
def get_date_range(begin,end):
need_date = []
append_timedate = datetime.timedelta(days=1)#用于增殖变量
while begin <= end:
need_date.append(begin)
begin_obj = datetime.datetime.strptime(begin,'%Y-%m-%d')#将输入封包成时间元组
begin = (begin_obj+append_timedate).strftime('%Y-%m-%d')#加上一天后解封
return need_date
begin_date = "2021-4-28"
end_date = "2021-5-3"
date_list = get_date_range(begin_date,end_date)
print(date_list)
输出结果为2021直接满到12-31
做了断点,bug测试时发现while当时间大于该值时仍然判定为True
故写以下代码测试
begin_date = "2021-5-1"
end_date = "2021-5-3"
if_outof_data = "2021-5-5"
if begin_date <= end_date:
print("证实可被对比")
if if_outof_data <= end_date:
print("若输出此结果则证明data大小判断无效")
结果为
"C:\Program Files\Python310\python.exe" G:/python基础题20道开始/025.py
证实可被对比
Process finished with exit code 0
离谱