Date sweep

Posted on May 17, 2006
Tags: python

On more than one occasion I've needed to iterate over all dates between two dates, and although the code and concept are trivial it does normally take up 20 minutes to find the exact classes to use, simply because it is a long time between uses. So here's the snippet, more for my own reference, but it will probably be useful to others too.

from datetime import date, timedelta
step = timedelta(1) # one day at a time
start = date(2005,1,1)
end = date.today()
d = start
while d <= end:
    print d
    d = d + step