Subtract Two DateTime objects – Python
Subtract Two DateTime objects – Python
When you subtract two datetime objects in python, you get a datetime.timedelta
object. You can then get the total_seconds()
for that timedelta object and check if that is greater than 3*3600
, which is the number of seconds for 3 hours. Example –
>>> a = datetime.datetime.now()
>>> b = datetime.datetime(2015,8,25,0,0,0,0)
>>> c = a - b
>>> c.total_seconds()
87062.729491
>>> c.total_seconds() > 3*3600
True
You can also just compare to another timedelta object
import datetime
if c >= datetime.timedelta(hours=3):
#do something
Subtract Two DateTime objects – Python
You can do this
three_hours = 3600*3 # in seconds
if c.total_seconds() >= three_hours:
# do stuff