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']