0
我想使用python將UTC格式日期格式轉換爲所需格式。我只需要格式的日期時間(2012年12月19日19:06:06 + 0000),我需要將其轉換爲我的格式(2012-12-07 19:06:06:546 +0000)使用python將utc格式轉換爲所需格式
代碼:
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')
結果:
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'
從蟒蛇的錯誤下面的鏈接表示,其固定的,但我不明白什麼是固定的,在哪個版本,我可以使用%Z
http://bugs.python.org/issue6641
我無法使用%z。有沒有其他的方式來處理?
如果可能的話,你可以改變你的Python版本。您可以在Python 3.2及更高版本中使用%z。 我已經在Python3.4中測試過OP的代碼,它工作正常。 http://stackoverflow.com/questions/2609259/converting-string-to-datetime-object-in-python – sk8asd123