Comparing a time delta in python

You’ll have to create a new timedelta with the specified amount of time:

d > timedelta(minutes=1)

Or this slightly more complete script will help elaborate:

import datetime
from time import sleep

start = datetime.datetime.now()
sleep(3)
stop = datetime.datetime.now()

elapsed = stop - start

if elapsed > datetime.timedelta(minutes=1):
    print "Slept for > 1 minute"

if elapsed > datetime.timedelta(seconds=1):
    print "Slept for > 1 second"

Output:

Slept for > 1 second

Leave a Comment