How Do I Retreive Future Dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yudesh
    New Member
    • Mar 2008
    • 6

    How Do I Retreive Future Dates

    Hi guys

    I am hoping that someone can help me with the following.

    I am a new developer to python and I have the following problem:
    I have been able to retrieve a certain date from an oracle database that I am connected to.
    This date is the basis for the following information that I need:
    - I need to get the date of the 3rd of the month after the date I retrieve.
    - I also need to get the 1st and last days of the month after the date that I retrieve.

    e.g - i got the following date 250208
    i need to get the 1st of march - 3rd march - 31st march

    This is an example, so I will be using variables to plug in any value I want.

    I have tried to see which date function could help, but with no success.

    Any assistance will be highly appreciated.

    Thanks
    Yudesh
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    have you tried using datetime module?

    here's some examples

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by Yudesh
      Hi guys

      I am hoping that someone can help me with the following.

      I am a new developer to python and I have the following problem:
      I have been able to retrieve a certain date from an oracle database that I am connected to.
      This date is the basis for the following information that I need:
      - I need to get the date of the 3rd of the month after the date I retrieve.
      - I also need to get the 1st and last days of the month after the date that I retrieve.

      e.g - i got the following date 250208
      i need to get the 1st of march - 3rd march - 31st march

      This is an example, so I will be using variables to plug in any value I want.

      I have tried to see which date function could help, but with no success.

      Any assistance will be highly appreciated.

      Thanks
      Yudesh
      The first thing you need to do is parse the string into a format that can be passed to datetime.date(). This returns a date object. To get the third of the next month, you can use the date method replace(). To get the last day of the next month, you can again use the date method replace() and subtract datetime.timede lta(days=3).[code=Python]def future_dates(da teStr):
      # dateStr is in the format '250208'
      d = datetime.date(i nt('20'+dateStr[4:]), int(dateStr[2:4]), int(dateStr[:2]))
      d1 = d.replace(month =d.month+1, day=3)
      d2 = d1.replace(mont h=d1.month+1)-datetime.timede lta(days=3)
      return d, d1, d2

      print future_dates('2 50208')[/code]
      Output:

      >>> (datetime.date( 2008, 2, 25), datetime.date(2 008, 3, 3), datetime.date(2 008, 3, 31))

      Comment

      • Yudesh
        New Member
        • Mar 2008
        • 6

        #4
        Hi

        Thanks a lot for the assistance - I have incorporated this into my script - I will now try to format the date in a way that is needed for my project.

        Thanks Again:


        Originally posted by bvdet
        The first thing you need to do is parse the string into a format that can be passed to datetime.date(). This returns a date object. To get the third of the next month, you can use the date method replace(). To get the last day of the next month, you can again use the date method replace() and subtract datetime.timede lta(days=3).[code=Python]def future_dates(da teStr):
        # dateStr is in the format '250208'
        d = datetime.date(i nt('20'+dateStr[4:]), int(dateStr[2:4]), int(dateStr[:2]))
        d1 = d.replace(month =d.month+1, day=3)
        d2 = d1.replace(mont h=d1.month+1)-datetime.timede lta(days=3)
        return d, d1, d2

        print future_dates('2 50208')[/code]
        Output:

        >>> (datetime.date( 2008, 2, 25), datetime.date(2 008, 3, 3), datetime.date(2 008, 3, 31))

        Comment

        Working...