如何在Python中使用箭头正确计算日期范围

px9o7tmv  于 4个月前  发布在  Python
关注(0)|答案(2)|浏览(67)

我有下面的一小段代码,我试图使用arrow计算今天昨天之间的天数,并确保结果准确。

import arrow

TODAY = arrow.now()

YESTERDAY = arrow.now().shift(days=-1)

result = TODAY - YESTERDAY

print("result: ", result)
print("number of days: ", result.days)
print("TODAY: ", TODAY)
print("YESTERDAY: ", YESTERDAY)

字符串
以下是我得到的结果:

result:  23:59:59.999912
number of days:  0     # this is because the result is not 24 but 23:59 instead... 
TODAY:  2022-05-16T11:54:03.332408+00:00
YEST:  2022-05-15T11:54:03.332496+00:00


有没有更好的方法,我错过了如何实现上述使用arrow具体?

fjnneemd

fjnneemd1#

TODAY.shift(days=-1)代替arrow.now().shift(days=-1)怎么样?这样,无论语句执行了多长时间,你的日期都将间隔1天。

k7fdbhmy

k7fdbhmy2#

我在我的系统上运行了几次,它总是出现

result:  1 day, 0:00:00
number of days:  1
TODAY:  2022-05-16T07:15:14.513011-05:00
YESTERDAY:  2022-05-15T07:15:14.513011-05:00

字符串
如果你改变了,你的机器会更好吗?

YESTERDAY = arrow.now().shift(days=-1)


这个吗

YESTERDAY = TODAY.shift(days=-1)

相关问题