Your first step should have been to look at the python datetime library.
In general, your first solution might look something like this:
date1 = datetime.date(2004, 9, 25) date2 = datetime.date(2004, 10, 8) day = datetime.timedelta(days=1) while date1 <= date2: print date1.strftime('%Y.%m.%d') date1 = date1 + day
(one note: this will obviously hide your date1 variable)
I will later reorganize this into a daterange function so that you can do something closer to what you did; it would look like
for d in daterange(date1, date2): print d.strftime('%Y.%m.%d')
Later, when you develop your python skills, he may like it like this:
for i in range((date2 - date1).days + 1): print (date1 + datetime.timedelta(days=i)).strftime('%Y.%m.%d')
Or is this what would be my latest version:
def daterange(d1, d2): return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1)) for d in daterange(date1, date2): print d.strftime('%Y.%m.%d')
source share