Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try about doing TODAY.shift(days=-1) instead of arrow.now().shift(days=-1). This way your dates will be exactly 1 day apart, no matter how long execution of statements took.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading