我正在尝试解析这个日期时间字符串,但还没有成功,我怎么能得到它?
d = '2014-05-01 18:10:38-04:00'
datetime.datetime.strptime(d,'%Y-%m-%d %H:%M:%S-%Z')
ValueError: time data '2014-05-01 18:10:38-04:00' does not match format '%Y-%m-%d %H:%M:%S%Z'
答案 0 :(得分:1)
您尝试过iso8601 lib吗?首先安装它:https://pypi.python.org/pypi/iso8601/
然后:
import iso8601
mydate = '2014-05-01 18:10:38-04:00'
iso8601.parse_date(mydate)
Out[3]: datetime.datetime(2014, 5, 1, 18, 10, 38, tzinfo=<FixedOffset '-04:00'>)
答案 1 :(得分:1)
您也可以使用python-dateutil
模块:
>>> from dateutil import parser
>>> d = '2014-05-01 18:10:38-04:00'
>>> parser.parse(d)
datetime.datetime(2014, 5, 1, 18, 10, 38, tzinfo=tzoffset(None, -14400))
另见: