gpt4 book ai didi

python - 如何将 python time.struct_time 对象转换为 ISO 字符串?

转载 作者:IT老高 更新时间:2023-10-28 20:39:01 26 4
gpt4 key购买 nike

我有一个 Python 对象:

time.struct_time(tm_year=2013, tm_mon=10, tm_mday=11, tm_hour=11, tm_min=57, tm_sec=12, tm_wday=4, tm_yday=284, tm_isdst=0)

我需要获得 ISO string :

'2013-10-11T11:57:12Z'

我该怎么做?

最佳答案

使用 time.strftime()可能是最简单的:

iso = time.strftime('%Y-%m-%dT%H:%M:%SZ', timetup)

演示:

>>> import time
>>> timetup = time.gmtime()
>>> time.strftime('%Y-%m-%dT%H:%M:%SZ', timetup)
'2013-10-11T13:31:03Z'

您也可以使用 datetime.datetime() 对象,它有一个 datetime.isoformat()方法:

>>> from datetime import datetime
>>> datetime(*timetup[:6]).isoformat()
'2013-10-11T13:31:03'

这错过了时区 Z 标记;你可以添加那个。

关于python - 如何将 python time.struct_time 对象转换为 ISO 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19319370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com