By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,403 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,403 IT Pros & Developers. It's quick & easy.

converting date

P: 1
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
Aug 3 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
It can be done like this:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'Fri, 29 Jul 2011 08:15:34 -0400'
  2. >>> s.split()
  3. ['Fri,', '29', 'Jul', '2011', '08:15:34', '-0400']
  4. >>> import time
  5. >>> t = time.strptime(' '.join(s.split()[:4]), '%a, %d %b %Y')
  6. >>> s1 = time.strftime("%Y-%m-%d ", t)+ " ".join(s.split()[4:])
  7. >>> s1
  8. '2011-07-29 08:15:34 -0400'
  9. >>> 
Aug 3 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.