Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 1st, 2005, 10:38 PM
 zci
Registered User

Join Date: Mar 2004
Posts: 17
Time spent in forums: 58 m 31 sec
Reputation Power: 0
Dates in between dates

I have a Start Date and an End Date.

Is there a way to determine each date between my Start and End date? So, essentially, the script would take in a start and end date and spit out the dates in between inclusive of the start and end date.

I'm thinking this can be done using the calendar module but I'm clueless on how to go about starting it. Any help would be appreciated. Thanks!

#2
February 1st, 2005, 11:37 PM
 Zone
Contributing User

Join Date: Jan 2005
Posts: 37
Time spent in forums: 1 Day 2 h 30 m 30 sec
Reputation Power: 9
Here's some ugly, inflexible code that does what I think you're asking for, using the calendar module. I didn't wring it out or anything, but it seems to work. A little extra logic in there and it would print the weekday as well.

I'm not sure if there's some other module out there that would handle this task a little cleaner, but you get the idea of how it can be done anyway. Hope this helps!

Code:
```import calendar

st_mo = int(raw_input("Gimme start month:"))
st_day = int(raw_input("Gimme start day:"))
st_year = int(raw_input("Gimme start year:"))

end_mo = int(raw_input("Gimme end month:"))
end_day = int(raw_input("Gimme end day:"))
end_year = int(raw_input("Gimme end year:"))

mo_ptr = st_mo
day_ptr = st_day
yr_ptr = st_year

mo_stop = 12
day_stop = calendar.monthrange(yr_ptr,mo_ptr)[1]
while yr_ptr < end_year:
outString = str(mo_ptr)+'/'+str(day_ptr)+'/'+str(yr_ptr)
print outString
day_ptr = day_ptr+1
if day_ptr > day_stop:
day_ptr = 1
mo_ptr = mo_ptr+1
if mo_ptr > mo_stop:
mo_ptr = 1
yr_ptr = yr_ptr + 1
day_stop = calendar.monthrange(yr_ptr,mo_ptr)[1]
else:
day_stop = calendar.monthrange(yr_ptr,mo_ptr)[1]

while mo_ptr < end_mo:
outString = str(mo_ptr)+'/'+str(day_ptr)+'/'+str(yr_ptr)
print outString
day_ptr = day_ptr + 1
if day_ptr > day_stop:
day_ptr = 1
mo_ptr = mo_ptr + 1
day_stop = calendar.monthrange(yr_ptr,mo_ptr)[1]

while day_ptr <= end_day:
outString = str(mo_ptr) + '/'+str(day_ptr)+'/'+str(yr_ptr)
print outString
day_ptr = day_ptr + 1```

#3
February 1st, 2005, 11:43 PM
 wx_uab
Contributing User

Join Date: Jun 2004
Location: Bimingham,AL
Posts: 68
Time spent in forums: 14 h 58 m 22 sec
Reputation Power: 10
just one way to do it

import timedate

def returnDates( start , end ) :
#start and end are objects of type timedate.date
datesInRange = []
interval = timedate.timedelta(days = 1 )
while start < end :
start = start + interval
datesInRange.append(start)
# returns list of objects of type date ( timedate.date)
return datesInRange

#4
February 1st, 2005, 11:45 PM
 wx_uab
Contributing User

Join Date: Jun 2004
Location: Bimingham,AL
Posts: 68
Time spent in forums: 14 h 58 m 22 sec
Reputation Power: 10
here's a simpler version

Code:
```import timedate

def returnDates( start , end ) :
#start and end are objects of type timedate.date
datesInRange = []
interval = timedate.timedelta(days = 1 )
while start < end :
start = start + interval
datesInRange.append(start)
# returns list of objects of type date ( timedate.date)
return datesInRange```

#5
February 2nd, 2005, 09:01 AM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Wx, your indentation seems to be broken: is this what you ment to post?

Code:
```import timedate

def returnDates( start , end ) :
#start and end are objects of type timedate.date
datesInRange = []
interval = timedate.timedelta(days = 1 )
while start < end :
start = start + interval
datesInRange.append(start)
#returns list of objects of type date ( timedate.date)
return datesInRange```

Mark.
__________________
programming language development: www.netytan.com Hula

#6
February 2nd, 2005, 04:07 PM
 wx_uab
Contributing User

Join Date: Jun 2004
Location: Bimingham,AL
Posts: 68
Time spent in forums: 14 h 58 m 22 sec
Reputation Power: 10
yeah mark . Thanks

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Dates in between dates