[Tutor] iterate over daterange
Kent Johnson
kent37 at tds.net
Sat Jan 7 15:15:54 CET 2006
captnswing wrote:
> Hello
> I have a startdate and an enddate and I want to iterate over all days
> in between the two
>
> .... there doesn't seem to be a range function for dates?!?
>
> i.e. currently I am going through integers with something like this:
>
> =================================
> startdate = datetime.date(2006,1,1)
> enddate = datetime.date(2006,10,19)
>
> for i in range((enddate-startdate).days + 1):
> currentdate = startdate + datetime.timedelta(days=i)
> ....
> =================================
>
> this seems so 'unpythonic', there surely must be a better way, no?
currentdate = datetime.date(2006,1,1)
enddate = datetime.date(2006,1,19)
while currentdate <= enddate:
print currentdate
currentdate += datetime.timedelta(days=1)
seems a little better to me.
Gustavo Niemeyer's dateutil package includes very flexible recurrence
rules - see
http://labix.org/python-dateutil#head-470fa22b2db72000d7abe698a5783a46b0731b57
Kent
More information about the Tutor
mailing list