import datetime
def list_date(start_date='2020-01-01', end_date='2020-01-31'):
#Convert str type to datetime type.
date_start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
date_end = datetime.datetime.strptime(end_date, '%Y-%m-%d')
#Calculate the interval between the start date and the end date.
days = (date_end - date_start).days
#The for statement is rotated by the interval and the day is added.
tmp_date = start_date
date = [tmp_date]
for i in range(days):
tmp_date += datetime.timedelta(days=1)
date.append(tmp_date)
return date
date = list_date(start_date='2020-01-01', end_date='2020-01-05')
print(date)
Execution result
[datetime.datetime(2020 1, 1, 0, 0),
datetime.datetime(2020 1, 2, 0, 0),
datetime.datetime(2020 1, 3, 0, 0),
datetime.datetime(2020 1, 4, 0, 0),
datetime.datetime(2020 1, 5, 0, 0)]
Recommended Posts