r/programminghelp • • Mar 31 '20

Python Iterating using python's datetime.date

So, I'm making a program that requires an array of each date between two given dates -

start = datetime.date(2020,1,1)
end = datetime.date(2020,12,31)
myArr = [all dates between (and including) start and end]

Is there a way to fill this array with every valid date between those two?

4 Upvotes

1 comment sorted by

View all comments

3

u/[deleted] Mar 31 '20

It should work

import datetime
import pprint

start = datetime.date(2020,2,10)
end = datetime.date(2020,10,21)


days = end - start
days = days.days

arr = [start + datetime.timedelta(days=x) for x in range(days+1)]

pprint.pprint(arr)