Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python - Check if current time is in a given range
from datetime import datetime, time
def is_time_between(begin_time, end_time):
# If check time is not given, default to current UTC time
check_time = datetime.utcnow().time()
if begin_time < end_time:
return begin_time <= check_time <= end_time
else: # crosses midnight
return check_time >= begin_time or check_time <= end_time
# Original test case from OP
print(is_time_between(time(10, 30), time(16, 30)))
# Test case when range crosses midnight
print(is_time_between(time(22, 0), time(4, 00)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.