I have a variable which is <type 'datetime.timedelta'>
and I would like to compare it against certain values.
Lets say d produces this datetime.timedelta
value 0:00:01.782000
I would like to compare it like this:
#if d is greater than 1 minute
if d>1:00:
print "elapsed time is greater than 1 minute"
I have tried converting datetime.timedelta.strptime()
but that does seem to work. Is there an easier way to compare this value?
but it requires python 2.7+
You just need to create
timedelta
object from scratch, comparison after that is trivial:Correct me if I'm wrong but I think that you could also use the following:
Instead of
You could say
You'll have to create a new
timedelta
with the specified amount of time:Or this slightly more complete script will help elaborate:
Output:
Slept for > 1 second