rooty
12/30/2012 - 10:26 PM

gistfile1.py

import datetime

def daterange(start, stop, step=datetime.timedelta(days=1), inclusive=True):
    # inclusive=False to behave like range by default
    if step.days > 0:
        while start < stop:
            yield start
            start = start + step
    elif step.days < 0:
        while start > stop:
            yield start
            start = start + step
    if inclusive and start == stop:
        yield start

Как юзать ? Вот например так:

from datetime import timedelta,date

end_date   = date.toady()
start_date = date.today()-timedelta(days=365)
dates = [x for x in daterange(start_date,
                              end_date,
                              step=timedelta(weeks=1),
                              inclusive=True)]