【问题标题】:converting json timestamp to datetime in python在python中将json时间戳转换为日期时间
【发布时间】:2021-11-21 18:04:35
【问题描述】:

如何在 Python 中将 '2021-11-21T18:57:18.000+01:00' 从 json 转换为 datetime?

  timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")

给我错误:

ValueError: 时间数据 '2021-11-21T18:57:18.000+01:00' 与格式 '%Y-%m-%dT%H:%M:%S.%fZ' 不匹配

【问题讨论】:

标签: python json datetime


【解决方案1】:

您的格式字符串略有错误。您需要%z 来解析区域偏移量:

from datetime import datetime

timestamp = '2021-11-21T18:57:18.000+01:00'
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")

print(timestamp)

结果:

2021-11-21 18:57:18+01:00

【讨论】:

  • 非常感谢!!真的很感激
猜你喜欢
  • 2019-02-03
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
相关资源
最近更新 更多