【问题标题】:How to add an hour to Twitter time (date (ISO 8601))? [closed]如何在 Twitter 时间(日期(ISO 8601))上增加一个小时? [关闭]
【发布时间】:2021-12-18 17:38:16
【问题描述】:

我只想在 Twitter 时间格式中添加一个小时。我得到的是:

start_time = '2021-03-11T00:00:00.000Z' 

我现在用谷歌搜索了几个小时,但没有任何效果。我总是收到时间日期与格式不匹配等错误。

【问题讨论】:

标签: python datetime twitter timedelta iso8601


【解决方案1】:

有很多方法可以做到这一点。这是一个...

安装 python-dateutil 然后:

from dateutil import parser
from datetime import timedelta

start_time = '2021-03-11T00:00:00.000Z'
d = parser.isoparse(start_time)
d += timedelta(hours=1)
print(f"{d.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3]}Z")

strftime 函数产生 6 个小数位。因此,要保持原始格式,您需要去掉最后 3 位数字,然后添加 Z

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多