How to convert Python's .isoformat() string back into datetime object [duplicate]

霸气de小男生 提交于 2019-11-28 17:42:19
Alex Urcioli

As it turns out, this is the current best "solution" to this question:

pip install python-dateutil

Then...

import datetime
import dateutil.parser

def getDateTimeFromISO8601String(s):
    d = dateutil.parser.parse(s)
    return d
user 12321

Try this:

>>> def gt(dt_str):
...     dt, _, us= dt_str.partition(".")
...     dt= datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
...     us= int(us.rstrip("Z"), 10)
...     return dt + datetime.timedelta(microseconds=us)

Usage:

>>> gt("2008-08-12T12:20:30.656234Z")
datetime.datetime(2008, 8, 12, 12, 20, 30, 656234)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!