Question : python and looping trough dates

hi,

If I have been given two dates....how to I write a loop that will loop trough and print all of the date?

e.g.
for date_beg to date_end:
    print date


     

Answer : python and looping trough dates

Here's one way to do it if you don't care about time (hours, minutes, etc), should get you started.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
## using datetime.date 
from datetime import datetime, date, timedelta
 
date_begin = date(2009, 6, 1)
date_end = datetime.now().date()
 
while date_begin <= date_end:
    print date_begin
    date_begin += timedelta(days=1)
 
## using datetime.datetime
from datetime import datetime, timedelta
 
date_begin = datetime(2009, 6, 1)
date_end = datetime.now()
 
while date_begin.date() <= date_end.date():
    print date_begin.date()
    date_begin += timedelta(days=1)
Open in New Window Select All
Random Solutions  
 
programming4us programming4us