-1

I have to find if a certain period of time in the format datetime.datetime.now().replace(microsecond=0) is greater than 5 minutes. How can I do that? This is my code:

ttime = datetime.now().replace(microsecond=0)
print "Current Time:- "+str(ttime)
a = raw_input("Press y and Enter")
if a=="y":
    print "OK!"
    ntime = datetime.now().replace(microsecond=0)
    difference = ntime-ttime
    if difference> #I don't know what to put!:
        print "Difference is greater than 5 min"
    else:
        print "Difference is not greater than 5 min"
Alok Naushad
  • 1,135
  • 1
  • 7
  • 9

2 Answers2

1

You can use difference = timedelta(minutes=5):

>>> from datetime import datetime, timedelta
>>> ttime = datetime.now().replace(microsecond=0)
>>> ntime = datetime.now().replace(microsecond=0)
>>> ntime - ttime > timedelta(minutes=5)
True

timedelta object initialized with 5 minutes:

>>> timedelta(minutes=5)
datetime.timedelta(0, 300)

To get more information read the docs about timedelta objects.

Dmitry
  • 1,896
  • 1
  • 16
  • 21
1
import datetime
ttime = datetime.datetime.now().replace(microsecond=0)
print "Current Time:- "+str(ttime)
a = raw_input("Press y and Enter")
if a=="y":
    print "OK!"
    ntime = datetime.datetime.now().replace(microsecond=0)
    if ttime < ntime - datetime.timedelta(minutes=5):
        print "Difference is greater than 5 min"
    else:
        print "Difference is not greater than 5 min"
HenryM
  • 5,563
  • 4
  • 44
  • 96