How to convert string to date object?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shivareddy
    New Member
    • Jan 2011
    • 6

    How to convert string to date object?

    i have date in string in this format
    eg. 1/20/21011 i need to convert it into the date object
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Code:
    >>> import time
    >>> s = "1/20/2011"
    >>> time.strptime(s, "%m/%d/%Y")
    (2011, 1, 20, 0, 0, 0, 3, 20, -1)
    >>>

    Comment

    • Shivareddy
      New Member
      • Jan 2011
      • 6

      #3
      thanks i tried it and it works fine i alos tried same for time also but i get problem when the time is provide in this format
      10:30:00 AM

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Code:
        >>> import time
        >>> s = "1/20/2011 10:30:00 AM"
        >>> time.strptime(s, "%m/%d/%Y %I:%M:%S %p")
        (2011, 1, 20, 10, 30, 0, 3, 20, -1)
        >>> time.strptime("1/20/2011 10:30:00 PM", "%m/%d/%Y %I:%M:%S %p")
        (2011, 1, 20, 22, 30, 0, 3, 20, -1)
        >>>

        Comment

        • Shivareddy
          New Member
          • Jan 2011
          • 6

          #5
          Thanks very much

          Comment

          Working...