gpt4 book ai didi

python - 如何在 python 中将日期时间对象移动 12 小时

转载 作者:太空狗 更新时间:2023-10-30 01:41:46 25 4
gpt4 key购买 nike

由于某种原因,日期时间对象伤了我的头。我正在写信来弄清楚如何将日期时间对象移动 12 小时。我还需要知道如何确定两个日期时间对象是否相差 1 分钟或更长时间。

最佳答案

datetime库有一个专门用于这种事情的 timedelta 对象:

import datetime

mydatetime = datetime.now() # or whatever value you want
twelvelater = mydatetime + datetime.timedelta(hours=12)
twelveearlier = mydatetime - datetime.timedelta(hours=12)

difference = abs(some_datetime_A - some_datetime_B)
# difference is now a timedelta object

# there are a couple of ways to do this comparision:
if difference > timedelta(minutes=1):
print "Timestamps were more than a minute apart"

# or:
if difference.total_seconds() > 60:
print "Timestamps were more than a minute apart"

关于python - 如何在 python 中将日期时间对象移动 12 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3660210/

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