Python recipe: Fetch all the days between two dates


I've added a new function to my python-math library that will return a list—technically a generator—of all of the days between two dates.

There might be a more obvious way to do this, but I don't know it. If you do, please let me know.

import datetime

def date_range(start_date, end_date):
    Returns a generator of all the days between two date objects.
    Results include the start and end dates.
    Arguments can be either datetime.datetime or date type objects.
    h3. Example usage
        >>> import datetime
        >>> import calculate
        >>> dr = calculate.date_range(,1,1),,1,3))
        >>> dr
        <generator object="object" at="at">
        >>> list(dr)
        [, 1, 1),, 1, 2),, 1, 3)]
    # If a datetime object gets passed in,
    # change it to a date so we can do comparisons.
    if isinstance(start_date, datetime.datetime):
        start_date =
    if isinstance(end_date, datetime.datetime):
        end_date =
    # Verify that the start_date comes after the end_date.
    if start_date > end_date:
        raise ValueError('You provided a start_date that comes after the end_date.')
    # Jump forward from the start_date...
    while True:
        yield start_date
        # ... one day at a time ...
        start_date = start_date + datetime.timedelta(days=1)
        # ... until you reach the end date.
        if start_date > end_date: