76

How do I iterate over a timespan after days, hours, weeks or months?

Something like:

for date in foo(from_date, to_date, delta=HOURS):
    print date

Where foo is a function, returning an iterator. I've been looking at the calendar module, but that only works for one specific year or month, not between dates.

martineau
  • 119,623
  • 25
  • 170
  • 301
sverrejoh
  • 16,464
  • 13
  • 41
  • 29

8 Answers8

122

Use dateutil and its rrule implementation, like so:

from dateutil import rrule
from datetime import datetime, timedelta

now = datetime.now()
hundredDaysLater = now + timedelta(days=100)

for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, until=hundredDaysLater):
    print dt

Output is

2008-09-30 23:29:54
2008-10-30 23:29:54
2008-11-30 23:29:54
2008-12-30 23:29:54

Replace MONTHLY with any of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY. Replace dtstart and until with whatever datetime object you want.

This recipe has the advantage for working in all cases, including MONTHLY. Only caveat I could find is that if you pass a day number that doesn't exist for all months, it skips those months.

Thomas Vander Stichele
  • 36,043
  • 14
  • 56
  • 60
  • 2
    http://niemeyer.net/python-dateutil explains how use it to retrieve "a date on the specified day of the month, unless it is beyond the end of month, in which case it will be the last day of the month". This fixes the one caveat you listed. As an example, now = datetime(2010, 8, 31, 1, 1, 1); for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, bymonthday=(31, -1), count=6): print dt – Smerity Dec 08 '10 at 00:14
50

I don't think there is a method in Python library, but you can easily create one yourself using datetime module:

from datetime import date, datetime, timedelta

def datespan(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate < endDate:
        yield currentDate
        currentDate += delta

Then you could use it like this:

>>> for day in datespan(date(2007, 3, 30), date(2007, 4, 3), 
>>>                     delta=timedelta(days=1)):
>>>     print day
2007-03-30
2007-03-31
2007-04-01
2007-04-02

Or, if you wish to make your delta smaller:

>>> for timestamp in datespan(datetime(2007, 3, 30, 15, 30), 
>>>                           datetime(2007, 3, 30, 18, 35), 
>>>                           delta=timedelta(hours=1)):
>>>     print timestamp
2007-03-30 15:30:00
2007-03-30 16:30:00
2007-03-30 17:30:00
2007-03-30 18:30:00
Dzinx
  • 55,586
  • 10
  • 60
  • 78
  • 5
    You can not specify a timedelta of one month – blueFast Aug 06 '14 at 12:02
  • To specify a delta of one month you can use relativedelta: `from dateutil.relativedelta import relativedelta`. And then you make `for month in datespan(start_date, end_date, delta=relativedelta(months=1))`. – angelacpd Jun 10 '21 at 17:53
9

I achieved this using pandas and datetime libraries as follows. It was much more convenient for me.

import pandas as pd
from datetime import datetime


DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

start_datetime = datetime.strptime('2018-05-18 00:00:00', DATE_TIME_FORMAT)
end_datetime = datetime.strptime('2018-05-23 13:00:00', DATE_TIME_FORMAT)

timedelta_index = pd.date_range(start=start_datetime, end=end_datetime, freq='H').to_series()
for index, value in timedelta_index.iteritems():
    dt = index.to_pydatetime()
    print(dt)
Thilina Madumal
  • 101
  • 1
  • 4
  • 2
    Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – Tim Diekmann May 23 '18 at 09:45
  • Great answer. This was exactly what I was looking for. I have just replaced H by m. ;) – chAlexey Feb 26 '20 at 15:43
  • Can you explain how that last iteration functions? e.g. value is not used anymore and I don't see why the index produces the output. – lpnorm Apr 30 '21 at 12:48
8

For iterating over months you need a different recipe, since timedeltas can't express "one month".

from datetime import date

def jump_by_month(start_date, end_date, month_step=1):
    current_date = start_date
    while current_date < end_date:
        yield current_date
        carry, new_month = divmod(current_date.month - 1 + month_step, 12)
        new_month += 1
        current_date = current_date.replace(year=current_date.year + carry,
                                            month=new_month)

(NB: you have to subtract 1 from the month for the modulus operation then add it back to new_month, since months in datetime.dates start at 1.)

giltay
  • 1,995
  • 1
  • 11
  • 5
  • 1
    Two things: - this code raises `ValueError` if you set start_date's day to a number that doesn't exist in every month - probably, exactly for that reason there is no month-timedelta; I would suggest using `timedelta(days=30)` for a good approximation. – Dzinx Sep 30 '08 at 17:29
  • 1
    Good catch. Sometimes, though, an approximation isn't good enough (my "pay my rent" reminder *has* to be at 6:30 on the first of every month). The recipe could be modified—probably with some extra state—to provide sane functionality, whatever that is. – giltay Oct 02 '08 at 14:53
  • 1
    You can also run into problems iterating year-by-year on February 29. – giltay Oct 19 '09 at 13:23
0

Month iteration approach:

def months_between(date_start, date_end):
    months = []

    # Make sure start_date is smaller than end_date
    if date_start > date_end:
        tmp = date_start
        date_start = date_end
        date_end = tmp

    tmp_date = date_start
    while tmp_date.month <= date_end.month or tmp_date.year < date_end.year:
        months.append(tmp_date)  # Here you could do for example: months.append(datetime.datetime.strftime(tmp_date, "%b '%y"))

        if tmp_date.month == 12: # New year
            tmp_date = datetime.date(tmp_date.year + 1, 1, 1)
        else:
            tmp_date = datetime.date(tmp_date.year, tmp_date.month + 1, 1)
    return months

More code but it will do fine dealing with long periods of time checking that the given dates are in order...

Rafa He So
  • 473
  • 3
  • 12
  • no need to use 'tmp' to swap two variables; you can do it in one step: "date_start, date_end = date_end, date_start" – Martin CR Jun 05 '20 at 14:46
0

Also can use the module arrow https://arrow.readthedocs.io/en/latest/guide.html#ranges-spans

>>> start = datetime(2013, 5, 5, 12, 30)
>>> end = datetime(2013, 5, 5, 17, 15)
>>> for r in arrow.Arrow.range('hour', start, end):
...     print(repr(r))
...
<Arrow [2013-05-05T12:30:00+00:00]>
<Arrow [2013-05-05T13:30:00+00:00]>
<Arrow [2013-05-05T14:30:00+00:00]>
<Arrow [2013-05-05T15:30:00+00:00]>
<Arrow [2013-05-05T16:30:00+00:00]>
Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
-1

This library provides a handy calendar tool: mxDateTime, that should be enough :)

dguaraglia
  • 5,774
  • 1
  • 26
  • 23
-3

You should modify this line to make this work correctly:

current_date = current_date.replace(year=current_date.year + carry,month=new_month,day=1)

;)