我该如何准确地将 datetime.utcnow()
返回的时间戳偏移任意时间(例如一天)?
例如:
now = datetime.utcnow().isoformat() + 'Z'
我需要上述抵消一天。当我的脚本进入夏令时转换时出现一个小问题,但我不需要看到它,但是因为它今天加载它也死了,因为 python 脚本现在在今天的日期工作时出错。
最佳答案
要简单地在 UTC 上添加一个特定的增量时间,请添加一个 timedelta :
from datetime import datetime, timedelta
now = (datetime.utcnow() + timedelta(days=3)).isoformat() + 'Z'
print(now)
输出:
2018-11-06T16:55:06.535804Z
有关带时区的 python 的更多信息,请访问 Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero offset)
使用 3.7 datetime.strptime
和 datetime.strftime
甚至可以将 01:30
识别为 %z
- up到 3.6 冒号会使它崩溃:)
关于python - Python 中的 UTCNow 偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133393/