gpt4 book ai didi

python - 如何将 "01 January 2016"转换为 UTC ISO 格式?

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:06 25 4
gpt4 key购买 nike

所以我有以下字符串:“2016 年 1 月 1 日”到 UTC ISO 日期格式?

我正在使用箭头模块和下面的代码,但它充满了错误,我想可能是,有一个更小更优雅的解决方案,因为 python 鼓励以优雅和更简单的方式做事,无论如何这是我的代码:

updateStr = " 01 January 2016" #Note the space at the beginning
dateList = updateStr.split[' ']
dateDict = {"day" : dateList[1],"month": months.index(dateList[2])+1, "year" : dateList[3]}
dateStr = str(dateDict['day']) + "-" + str(dateDict["month"]) + "-" + str(dateDict["year"])
dateISO = arrow.get(dateStr, 'YYYY-MM-DD HH:mm:ss')

请帮助我,我必须将其转换为 UTC ISO 格式,此外月份是一年中月份的列表。

最佳答案

您可以使用 datetime :

>>> updateStr = " 01 January 2016"
>>> import datetime as dt
>>> dt.datetime.strptime(updateStr, " %d %B %Y")
datetime.datetime(2016, 1, 1, 0, 0)
>>> _.isoformat()
'2016-01-01T00:00:00'

请记住,这是一个没有时区的“天真”对象。查看pytz优雅地处理时区,或者只添加适当的 utcoffset到 UTC 的日期时间对象。

关于python - 如何将 "01 January 2016"转换为 UTC ISO 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37500384/

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