converting date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clayton Munsamy
    New Member
    • Aug 2011
    • 1

    converting date

    Hi Guys

    I have a date in string format that i need to convert into date format

    Eg: Fri, 29 Jul 2011 08:15:34 -0400

    i need to convert it to a normal date and time format eg: 2011-07-29 08:15:34 -0400

    i am new to python can you please assist me
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It can be done like this:
    Code:
    >>> s = 'Fri, 29 Jul 2011 08:15:34 -0400'
    >>> s.split()
    ['Fri,', '29', 'Jul', '2011', '08:15:34', '-0400']
    >>> import time
    >>> t = time.strptime(' '.join(s.split()[:4]), '%a, %d %b %Y')
    >>> s1 = time.strftime("%Y-%m-%d ", t)+ " ".join(s.split()[4:])
    >>> s1
    '2011-07-29 08:15:34 -0400'
    >>>

    Comment

    Working...