I have two set times and I am trying to see if the current time is within those times.
When I attempt to do this I get the error
TypeError: '<=' not supported between instances of 'datetime.time' and 'str'
Below is the code that retrieves this error, How would I properly compare between datetime and time?
from datetime import datetime
from datetime import time
start = time(6, 35, 0)
end = time(12, 55, 0)
current = datetime.now().strftime('%H:%M:%S')
if start <= current <= end:
print(start, end, current)
CodePudding user response:
Do not cast your time to str with strftime
. You can use time()
function of datetime.datetime
to get time of timestamp. The thing you need is
from datetime import datetime
from datetime import time
start = time(6, 35, 0)
end = time(12, 55, 0)
current = datetime.now().time()
if start <= current <= end:
print(start, end, current)