code_junkie

How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?

Nov 11th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. import datetime, dateutil.parser
  2.  
  3. d = dateutil.parser.parse('2008-09-26T01:51:42.000Z')
  4. print d.strftime('%m/%d/%Y') #==> '09/26/2008'
  5.  
  6. >>> import time
  7. >>> timestamp = "2008-09-26T01:51:42.000Z"
  8. >>> ts = time.strptime(timestamp[:19], "%Y-%m-%dT%H:%M:%S")
  9. >>> time.strftime("%m/%d/%Y", ts)
  10. '09/26/2008'
  11.  
  12. def datechange(datestr):
  13. dateobj=datestr.split('-')
  14. y=dateobj[0]
  15. m=dateobj[1]
  16. d=dateobj[2]
  17. datestr=d +'-'+ m +'-'+y
  18. return datestr
Add Comment
Please, Sign In to add comment