Andrew Durdin adurdin at
Wed Jul 14 07:38:03 CEST 2004

On Mon, 12 Jul 2004 11:47:04 -0700, Robert Brewer <fumanchu at> wrote:
> def daterange(start, end):
>     for day in xrange((end - start).days):
>         yield start + datetime.timedelta(day)

How about a more general class to represent a date/time range
(essentially a timedelta with a starting datetime), with methods
returning lists/generators for different size increments. Something
like the following comes to my mind:
class datetimerange:
   def __init__(self, first, last):
      self.start_date = first
      self.date_delta = (last - first)

   def days(self, step=1):
      for day in range(0, self.date_delta.days, step):
         yield self.start_date + datetime.timedelta(days=day)

   def weeks(self, step=1):
      for day in range(0, self.date_delta.days, 7*step):
         yield self.start_date + datetime.timedelta(days=day)

   def hours(self, step=1):

   def minutes(self, step=1):


Then your loop would look like the following:

for day in datetime.datetimerange(first_date, last_date).days:

   -- Andrew

More information about the Python-list mailing list