如何将 Python 中的日期转换为 ISO 格式,最后是 Z?

How to convert date to ISO format in Python With the Z at the end?

如何将 Python 末尾带 Z 的日期转换为 ISO 格式?

日期是:

2020-11-01 01:11:39

如何将其转换为:

2020-11-01T01:11:39Z

?

请帮帮我。

编辑: How do I parse an ISO 8601-formatted date? 没有回答我的问题,因为我正在尝试 CONVERT 正常日期到 ISO 日期。

假设您从 datetime 对象开始,您可以在此处使用 strftime() 函数:

from datetime import datetime
now = datetime.now()
dt_out = now.strftime("%Y-%m-%dT%H:%M:%SZ")
print(dt_out)  # 2021-03-07T07:39:22Z