python3 获取两个日期字符串时间段内所有日期集合
1.代码
# 根据开始日期、结束日期返回这段时间里所有天的集合
def getDatesByTimes(sDateStr, eDateStr):
list = []
datestart = datetime.datetime.strptime(sDateStr, '%Y-%m-%d')
dateend = datetime.datetime.strptime(eDateStr, '%Y-%m-%d')
list.append(datestart.strftime('%Y-%m-%d'))
while datestart < dateend:
datestart += datetime.timedelta(days=1)
list.append(datestart.strftime('%Y-%m-%d'))
return list
2.测试结果
测试代码
print(getDatesByTimes('2019-2-26','2019-3-25'))
测试结果
['2019-02-26', '2019-02-27', '2019-02-28', '2019-03-01', '2019-03-02', '2019-03-03', '2019-03-04',
'2019-03-05', '2019-03-06', '2019-03-07', '2019-03-08', '2019-03-09', '2019-03-10', '2019-03-11',
'2019-03-12', '2019-03-13', '2019-03-14', '2019-03-15', '2019-03-16', '2019-03-17', '2019-03-18',
'2019-03-19', '2019-03-20', '2019-03-21', '2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25']