I have the following method:
# last_updated is a datetime() object, representing the last time this program ran
def time_diff(last_updated):
day_period =
Just to clear our some things, because I don't think all of us make use of the time
Python lib.
When you use datetime
, which is especially in Django a very common practice, if you do the comparison like this:
if (now - then) > DAY:
it will hugely fail. This is because you can't compare datetime.timedelta
to int
.
The solution to this is to convert your objects to seconds.
For example:
from datetime import datetime
then = datetime_object
now = datetime.now()
if (now - then).total_seconds() > NUMBER_OF_SECONDS:
# do something
Hope I helped out whoever faced issues on that.
Cheers