PythonLearn
PythonLearn

Reputation: 97

ValueError: unconverted data remains: :00 when i want compare two times

I wanna compare two times and i have an error

from time import strftime
from datetime import datetime
addtime = "18:00"
timeformat = '%H:%M'
now = strftime(timeformat)
print "Time: " + now
checkwhen = datetime.strptime(now, timeformat) - datetime.strptime(addtime, timeformat)
print "Added: "+str(checkwhen)+" ago" 
timecheck = str("00:15")

if (datetime.strptime(str(checkwhen), timeformat) < datetime.strptime(timecheck, timeformat)):
    print "ALERT!!!"

Why output from checkwhen is 0:07:00, i set timeformat to '%H:%M'. I think here is my problem, but i dont know have i can fix it

error: : unconverted data remains: :00

Upvotes: 0

Views: 1510

Answers (1)

pramod
pramod

Reputation: 1493

You can format timedelta object, so you have do that thing manually.

timecheck = str("00:15")
checkwhen = ':'.join(str(checkwhen).split(':')[:-1])
if (datetime.strptime(str(checkwhen), timeformat) < datetime.strptime(timecheck, timeformat)):
    print "ALERT!!!"

Upvotes: 1

Related Questions