Date Time in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parthpatel
    New Member
    • Apr 2007
    • 14

    Date Time in Python

    Suppose i have a present date and i want to check the date after 90 days than by which method i can do that
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by parthpatel
    Suppose i have a present date and i want to check the date after 90 days than by which method i can do that
    you can use the datetime module
    Code:
    >>> import datetime
    >>> nowdate = datetime.date(2007, 1, 1)
    >>> ninety_days = datetime.timedelta(days=90)
    >>> after_90 = nowdate + ninety_days
    >>> after_90
    datetime.date(2007, 4, 1)
    >>> after_90.ctime()
    'Sun Apr  1 00:00:00 2007'

    Comment

    • sangeeth
      New Member
      • Dec 2006
      • 23

      #3
      Code:
      import time
      
      def parseDate(date):
        t = time
        structT = t.strptime(date,'%Y/%m/%d')
        objT = t.mktime(structT)
        return objT
      
      
      if __name__ == '__main__':
              t = time
              t1 = parseDate('2007/04/06')
      
              t2 = t1 + 90*24*60*60
              x = t.localtime(t2)
              print str(t.strftime('%Y/%m/%d',x))
      In the above program parseDate() takes date in a specific format and returns the date after 90 days in the same format..
      You can customize it according to ur needs

      hope this was helpful

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by sangeeth
        Code:
        import time
        
        def parseDate(date):
          t = time...
        ....
        the above is shortened like this
        Code:
        import time as t

        Comment

        Working...