What would be the best way to see if the current time lies between say 10:30 AM
and 4:30 PM
.
I could think of the following, not sure how correct:
from datetime import datetime
nw = datetime.now()
hrs = nw.hour;mins = nw.minute;secs = nw.second;
zero = timedelta(seconds = secs+mins*60+hrs*3600)
st = nw - zero # this take me to 0 hours.
time1 = st + timedelta(seconds=10*3600+30*60) # this gives 10:30 AM
time2 = st + timedelta(seconds=16*3600+30*60) # this gives 4:30 PM
if nw>= time1 or nw <= time2:
print "yes, within the interval"
Please let me know if this the correct approach, can something better be written?
My original answer focused very specifically on the question as posed and didn't accommodate time ranges that span midnight. As this is still the accepted answer 6 years later, I've incorporated @rouble's answer below that expanded on mine to support midnight.
from datetime import datetime, time
def is_time_between(begin_time, end_time, check_time=None):
# If check time is not given, default to current UTC time
check_time = check_time or datetime.utcnow().time()
if begin_time < end_time:
return check_time >= begin_time and check_time <= end_time
else: # crosses midnight
return check_time >= begin_time or check_time <= end_time
# Original test case from OP
is_time_between(time(10,30), time(16,30))
# Test case when range crosses midnight
is_time_between(time(22,0), time(4,00))
I still stick to my original comment below that most applications of this logic would probably be better suited with datetime
objects where crossing midnight is reflected as a date change anyway.
The accepted solution does not work with overnight times:
def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
here's little example for @rouble's answer:
from datetime import datetime
def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else: #Over midnight
return nowTime >= startTime or nowTime <= endTime
timeStart = '3:00PM'
timeEnd = '11:00AM'
timeNow = '2:59AM'
timeEnd = datetime.strptime(timeEnd, "%I:%M%p")
timeStart = datetime.strptime(timeStart, "%I:%M%p")
timeNow = datetime.strptime(timeNow, "%I:%M%p")
print(isNowInTimePeriod(timeStart, timeEnd, timeNow))
Take a look in py-time-between package: https://pypi.org/project/py-time-between/
Test cases:
from datetime import time
from timebetween import is_time_between
def test_is_time_between():
t, s, e = time(0), time(0), time(0)
assert is_time_between(t, s, e)
t, s, e = time(0, 0, 0, 1), time(0), time(0, 0, 0, 2)
assert is_time_between(t, s, e)
t, s, e = time(0, 0, 0, 1), time(0, 0, 0, 1), time(0, 0, 0, 2)
assert is_time_between(t, s, e)
t, s, e = time(0, 0, 0, 2), time(0, 0, 0, 1), time(0, 0, 0, 2)
assert is_time_between(t, s, e)
t, s, e = time(0, 0, 1), time(23, 59, 59), time(0, 0, 2)
assert is_time_between(t, s, e)
t, s, e = time(12, 0, 0), time(23, 59, 59), time(0, 0, 0)
assert is_time_between(t, s, e)
t, s, e = time(23, 59, 57), time(23, 59, 59), time(23, 59, 57)
assert is_time_between(t, s, e)
t, s, e = time(23, 59, 58), time(23, 59, 59), time(23, 59, 57)
assert not is_time_between(t, s, e)
t, s, e = time(22), time(22), time(5, 59, 59)
assert is_time_between(t, s, e)