How To Convert String To Date?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aobaidat79
    New Member
    • Feb 2007
    • 2

    How To Convert String To Date?

    Dear Freinds
    I have string contains the date like "21Jun2000"
    What I want to do is to convert that string into date in the following format
    21 Jun 2000

    Plesae help me as soon as possible
    'Thank you very much
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Aobaidat79
    Dear Freinds
    I have string contains the date like "21Jun2000"
    What I want to do is to convert that string into date in the following format
    21 Jun 2000

    Plesae help me as soon as possible
    'Thank you very much
    If the format is constant you could

    >>> date = "21Jun2000"
    >>> day = date[:2]
    >>> month = date[2:5]
    >>> year = date[5:]
    >>> print day, month, year
    21 Jun 2000

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      the usual way to do it for any date format string is using time.strptime and time.strftime.j ust an example
      Code:
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import time
      >>> datestring = "21Jun2000"
      >>> c = time.strptime(datestring,"%d%b%Y")
      >>> time.strftime("%d %b %Y",c)
      '21 Jun 2000'
      >>>

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by ghostdog74
        the usual way to do it for any date format string is using time.strptime and time.strftime.j ust an example
        Code:
        Type "help", "copyright", "credits" or "license" for more information.
        >>> import time
        >>> datestring = "21Jun2000"
        >>> c = time.strptime(datestring,"%d%b%Y")
        >>> time.strftime("%d %b %Y",c)
        '21 Jun 2000'
        >>>
        Hey! That's awesome. I always thought that you needed a date.date object for that to work.

        Comment

        • Aobaidat79
          New Member
          • Feb 2007
          • 2

          #5
          Originally posted by ghostdog74
          the usual way to do it for any date format string is using time.strptime and time.strftime.j ust an example
          Code:
          Type "help", "copyright", "credits" or "license" for more information.
          >>> import time
          >>> datestring = "21Jun2000"
          >>> c = time.strptime(datestring,"%d%b%Y")
          >>> time.strftime("%d %b %Y",c)
          '21 Jun 2000'
          >>>
          You are a smart person
          Thank you all

          Comment

          Working...