【问题标题】:Transform datetimes in python在python中转换日期时间
【发布时间】:2021-09-29 09:19:41
【问题描述】:

如何像这样转换字符串日期时间:

"2018-07-13T00:00:00+00:00"

喜欢这个吗?

"2018-07-13T00:00:00.000000+00:00"

【问题讨论】:

标签: python datetime iso8601


【解决方案1】:

试试这个:

from datetime import datetime

datetime_string = "2018-07-13T00:00:00+00:00"
new_datetime_string = datetime.fromisoformat(datetime_string).isoformat(timespec="microseconds")

在 python V3.7 之前:

date_time = datetime.strptime(datetime_string, '%Y-%m-%dT%H:%M:%S%z').strftime("%Y-%m-%dT%H:%M:%S.%f%z")

【讨论】:

  • 不错!但请注意,fromisoformat() 是 3.7 版中的新增内容。
  • @UlfR 你是对的。答案已更新。
  • 请注意,strptime 解析指令将使其特定于输入字符串中的某种格式
猜你喜欢
  • 2010-12-28
  • 1970-01-01
  • 2018-07-25
  • 2018-11-21
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多