How to find if 24 hrs have passed between datetimes in Python?


To find out if 24 hrs have passed between datetimes in Python, you will need to do some date math in Python. So if you have 2 datetime objects, you'll have to subtract them and then take the timedelta object you get as a result and use if for comparision. You can't directly compare it to int, so you'll need to first extract the seconds from it. 

example

from datetime import datetime
NUMBER_OF_SECONDS = 86400 # seconds in 24 hours
first = datetime(2017, 10, 10)
second = datetime(2017, 10, 12)
if (first - second).total_seconds() > NUMBER_OF_SECONDS:
  print("its been over a day!")

Output

This will give the output −

its been over a day!

Updated on: 19-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements