![](/img/trans.png)
[英]How to convert “Tue Aug 25 10:00:00 2015” this time stamp to “2015-08-25 10:00” in python
[英]How to convert “Tue Feb 25 2014 00:00:00 GMT+0530 (IST)” to python datetime object?
我對Tue Feb 25 2014 00:00:00 GMT+0530 (IST)
的日期格式有一些疑問。
Tue Feb 25 2014 00:00:00
是指GMT
還是IST
datetime
。DD-MM-YY,HH:MM:SS
格式。 這是我試圖轉換為python datetime
::
但是當我嘗試使用%z
時出現錯誤:
>>> time_format="%a %b %d %Y %H:%M:%S GMT%z (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT+0530 (IST)"
>>> mydate=datetime.strptime(v,time_format)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S GMT%z (%Z)'
但這有效:
>>> time_format="%a %b %d %Y %H:%M:%S GMT (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT (IST)"
>>> datetime.strptime(v,time_format)
datetime.datetime(2014, 2, 25, 0, 0)
但仍然不了解TIMEZONE
。
在系統終端
easy_install python-dateutil
在 python 外殼中
from dateutil import parser as date_parser
print date_parser.parse("Tue Feb 25 2014 00:00:00 GMT+0530 (IST)")
Dateutil 的 parse 通常可以將您扔給它的任何內容解析為 Python 日期時間。
我認為最好的方法是嘗試這樣的事情,特別是如果你想避免使用外部庫,例如在 Python 3.6.9 中:
datetime.strptime(v,"%a %b %d %Y %H:%M:%S %Z%z (IST)")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.