262

For example:

from datetime import date

d1 = date(2008,8,15)
d2 = date(2008,9,15)

I'm looking for simple code to print all dates in-between:

2008,8,15  
2008,8,16  
2008,8,17  
...  
2008,9,14  
2008,9,15

Thanks

doniyor
  • 36,596
  • 57
  • 175
  • 260
zetah
  • 2,981
  • 5
  • 19
  • 12

5 Answers5

491

I came up with this:

from datetime import date, timedelta

start_date = date(2008, 8, 15) 
end_date = date(2008, 9, 15)    # perhaps date.now()

delta = end_date - start_date   # returns timedelta

for i in range(delta.days + 1):
    day = start_date + timedelta(days=i)
    print(day)

The output:

2008-08-15
2008-08-16
...
2008-09-13
2008-09-14
2008-09-15

Your question asks for dates in-between but I believe you meant including the start and end points, so they are included. To remove the end date, delete the "+ 1" at the end of the range function. To remove the start date, insert a 1 argument to the beginning of the range function.

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
  • 1
    Hello all, i tried it but It prints only the last day, i need full list day dates_list = [] result = d1 + td(days=i) dates_list.append(result) print(result) It prints only the last day, how to solve this? – Droid May 18 '15 at 21:27
  • 1
    Sounds like your loop is wrong somehow. Please reply with the value of your `range(delta.days)`. – Gringo Suave May 30 '15 at 02:12
  • Timedelta with days always adds multiples of 24h. So if you cross a switch to day light savings time, you are off by one hour (each being at 1am). Going in the other direction you even have a different day, each at 11pm. One of the reasons to use UTC... – Jochen Feb 28 '20 at 23:41
  • This uses the Date class, so time is not a factor. – Gringo Suave Feb 29 '20 at 00:17
  • @GringoSuave this is not working for Leap years,it add the Feb 29 even in Leap years, Can you fix ? – Vignesh Oct 14 '20 at 13:51
  • @GringoSuave If I want to get date list as ['20190322', '20190323',.......], what to do? Thankyou :-) – R S John Nov 19 '21 at 08:40
  • 1
    @RSJohn , look up `python strftime`. If you don't mind dashes in between `date.isoformat()` is the best way. – Gringo Suave Nov 21 '21 at 21:21
58

Using a list comprehension:

from datetime import date, timedelta

d1 = date(2008,8,15)
d2 = date(2008,9,15)

# this will give you a list containing all of the dates
dd = [d1 + timedelta(days=x) for x in range((d2-d1).days + 1)]

for d in dd:
    print d

# you can't join dates, so if you want to use join, you need to
# cast to a string in the list comprehension:
ddd = [str(d1 + timedelta(days=x)) for x in range((d2-d1).days + 1)]
# now you can join
print "\n".join(ddd)
Hugo Rodger-Brown
  • 11,054
  • 11
  • 52
  • 78
18

Essentially the same as Gringo Suave's answer, but with a generator:

from datetime import datetime, timedelta


def datetime_range(start=None, end=None):
    span = end - start
    for i in xrange(span.days + 1):
        yield start + timedelta(days=i)

Then you can use it as follows:

In: list(datetime_range(start=datetime(2014, 1, 1), end=datetime(2014, 1, 5)))
Out: 
[datetime.datetime(2014, 1, 1, 0, 0),
 datetime.datetime(2014, 1, 2, 0, 0),
 datetime.datetime(2014, 1, 3, 0, 0),
 datetime.datetime(2014, 1, 4, 0, 0),
 datetime.datetime(2014, 1, 5, 0, 0)]

Or like this:

In []: for date in datetime_range(start=datetime(2014, 1, 1), end=datetime(2014, 1, 5)):
   ...:     print date
   ...:     
2014-01-01 00:00:00
2014-01-02 00:00:00
2014-01-03 00:00:00
2014-01-04 00:00:00
2014-01-05 00:00:00
cieplak
  • 1,103
  • 1
  • 11
  • 13
  • 1
    I prefer the generator idea, it can always be a helpers function somewhere in the code; However, it certainly lack type and sanity check. `start` and `end` args default set to None is asking for errors. – Ramez Ashraf Jun 25 '17 at 11:52
  • 1
    `for i in xrange(span.days + 1):` is causing errors, had to change it to `or i in range(span.days + 1):` – LucasSeveryn Aug 20 '20 at 07:17
  • The other answer is a generator on Python 3, this Python 2 code is now obsolete. – Gringo Suave Oct 16 '20 at 18:50
8
import datetime

d1 = datetime.date(2008,8,15)
d2 = datetime.date(2008,9,15)
diff = d2 - d1
for i in range(diff.days + 1):
    print (d1 + datetime.timedelta(i)).isoformat()
ine
  • 14,014
  • 8
  • 55
  • 80
4
import datetime

begin = datetime.date(2008, 8, 15)
end = datetime.date(2008, 9, 15)

next_day = begin
while True:
    if next_day > end:
        break
    print next_day
    next_day += datetime.timedelta(days=1)
develerx
  • 600
  • 5
  • 13