【发布时间】:2018-06-12 22:45:42
【问题描述】:
我的日期格式如下:"2018-06-12T13:58:36.663550655Z"
我想将此字符串转换为 Unix 时间戳。
这是我的代码:
from datetime import datetime
date = '2018-06-12T14:03:35.306662173Z'
time = datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
unixTime = datetime.utcfromtimestamp(time)
运行时出现错误:
ValueError: 时间数据 '2018-06-12T14:03:35.306662173Z' 不匹配 格式 '%Y-%m-%dT%H:%M:%S.%fZ'
如果有人可以帮助我,那就太好了! 谢谢!
【问题讨论】:
-
仅供参考,
datetime.strptime()返回一个datetime对象,而不是 posix 时间戳。 -
我不确定,但我认为您需要
time = datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%f%Z")来处理时区标识符
标签: python python-3.x