Date Range
Write a Python function called daterange(start, end)
that takes two datetime.date
objects as arguments and returns a list of all the dates between them. The list should include the start date but not the end date.
To solve this problem, you can follow these steps:
- Use
datetime.timedelta.days
to get the number of days between start
and end
.
- Use
int()
to convert the result to an integer and range()
to iterate over each day.
- Use a list comprehension and
datetime.timedelta
to create a list of datetime.date
objects.
from datetime import timedelta, date
def daterange(start, end):
return [start + timedelta(n) for n in range(int((end - start).days))]
from datetime import date
daterange(date(2020, 10, 1), date(2020, 10, 5))
## [date(2020, 10, 1), date(2020, 10, 2), date(2020, 10, 3), date(2020, 10, 4)]