用于将字符串数据转换为基于UTC的日期时间的日期时间格式

时间:2016-11-02 19:02:26

标签: python datetime type-conversion

我有这个日期数据:

timestamp="2016-11-01T18:18:46.5035795Z"

我正在寻找正确的 dtfmt ,因此我可以将上面的数据转换为正确的数据时间值,并将 UTC 设置为时区(tzinfo)

dt = datetime.datetime.strptime(timestamp, dtfmt)

有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:0)

Object.keys模块非常适合此

>>> from dateutil import parser
>>> parser.parse(timestamp)
datetime.datetime(2016, 11, 1, 18, 18, 46, 503579, tzinfo=tzutc())

然后

datetime

如果没有dateutil模块,则必须使用正则表达式去除字符串的微秒部分中的额外字符,然后使用与字符串匹配的正确日期格式将其转换为>>> import re >>> timestamp = re.findall('.*[0-9]{6}', timestamp)[0] + 'Z' >>> datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ') datetime.datetime(2016, 11, 1, 18, 18, 46, 503579) 对象

public class Thing