I found some nice examples to check, if a time is in a specific range, like this one:
now_time = datetime.datetime.now().time()
start = datetime.time(17, 30)
end = datetime.time(4, 00)
if start <= now_time <= end:
In my case, I want to check, if the current time is between 17:30 and 04 in the morning. How am I able to solve this?
17:30 comes after 4:00, so anything with start <= x <= end
will evaluate to false, because it implies that end (4:00) is larger than start (17:30), which is never true.
What you must do instead is check whether it's past 17:30 or it's before 4:00:
import datetimenow_time = datetime.datetime.now().time()
start = datetime.time(17, 30)
end = datetime.time(4, 00)
if now_time >= start or now_time <= end:print('true')
else:print('false')