[英]how to calculate date range correctly using arrow in python
I have the small snippet of codes below, I am trying to calculate the number of days between today and yesterday using arrow
and make sure the result is accurate.我有下面的一小段代码,我正在尝试使用
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)
here is the result that i am getting:这是我得到的结果:
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
Is there a better way that i am missing on how to achieve the above using arrow
specifically?关于如何专门使用
arrow
实现上述目标,有没有更好的方法?
Try about doing TODAY.shift(days=-1)
instead of arrow.now().shift(days=-1)
.尝试做
TODAY.shift(days=-1)
而不是arrow.now().shift(days=-1)
。 This way your dates will be exactly 1 day apart, no matter how long execution of statements took.这样,无论语句的执行花费了多长时间,您的日期都将恰好相隔 1 天。
I ran it several times on my system and it always came up with我在我的系统上运行了几次,它总是想出
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
Does it get better on your machine if you change如果你改变它在你的机器上会变得更好吗
YESTERDAY = arrow.now().shift(days=-1)
to this?到这个?
YESTERDAY = TODAY.shift(days=-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.