3

我对 dateformat 有一些疑问Tue Feb 25 2014 00:00:00 GMT+0530 (IST)

  • 确实Tue Feb 25 2014 00:00:00意味着GMTIST
  • 是否可以将其转换为 python datetime
  • 是否可以将其转换为DD-MM-YY,HH:MM:SSGMT 格式。

这是我试图转换为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

4

2 回答 2

4

在系统终端

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 的解析通常可以将您扔给它的任何内容解析为 Python 日期时间。

于 2014-03-18T16:06:54.133 回答
2

我认为最好的方法是尝试这样的事情,特别是如果你想避免使用外部库,例如在 Python 3.6.9 中:

datetime.strptime(v,"%a %b %d %Y %H:%M:%S %Z%z (IST)")
于 2019-11-07T14:42:39.987 回答