0

I am trying to convert UTC format date format to required format using python. Simply I have the datetime in the format(Fri Dec 07 19:06:06 +0000 2012), I need to convert this into my format(2012-12-07 19:06:06:546 +0000)

Code:

created_at  = "Fri Dec 07 19:06:06 +0000 2012"
d = datetime.strptime(created_at, '%a %b %d %H:%M:%S %z %Y')
date_object = d.strftime('%y-%m-%d %H:%M:%S')

result:

ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'

The below link from python bugs says its fixed but i didn understand what is fixed and in which version i can use %z

http://bugs.python.org/issue6641

I am not able to use %z . Is there any other way to handle this??

user2695817
  • 121
  • 1
  • 7
  • If possible you could change your Python version. You can use %z in Python 3.2 and greater. I've tested OP's code in Python3.4 and it works fine. http://stackoverflow.com/questions/2609259/converting-string-to-datetime-object-in-python – sk8asd123 Apr 22 '14 at 20:00

1 Answers1

0

Do it like that:

from datetime import datetime    
TIMESTAMP = datetime.utcnow().strftime('%d/%m/%Y %H:%M:%S')
EdChum
  • 376,765
  • 198
  • 813
  • 562
Satyam Saxena
  • 581
  • 2
  • 10