#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    37
    Rep Power
    10
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11

    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11

    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    yeah mark . Thanks

IMN logo majestic logo threadwatch logo seochat tools logo