Given a date range how can we break it up into N contiguous sub-intervals?

I would actually follow a different approach and rely on timedelta and date addition to determine the non-overlapping ranges

Implementation

def date_range(start, end, intv):
    from datetime import datetime
    start = datetime.strptime(start,"%Y%m%d")
    end = datetime.strptime(end,"%Y%m%d")
    diff = (end  - start ) / intv
    for i in range(intv):
        yield (start + diff * i).strftime("%Y%m%d")
    yield end.strftime("%Y%m%d")

Execution

>>> begin = '20150101'
>>> end = '20150228'
>>> list(date_range(begin, end, 4))
['20150101', '20150115', '20150130', '20150213', '20150228']

Leave a Comment

techhipbettruvabetnorabahisbahis forumu