1đź‘Ť
âś…
Here’s a quick and dirty solution if i understood what you want. You can just check if the time is between the hours the business is open / closed before appending to the list.
from datetime import datetime as d, timedelta, time
import datetime as o_d
time_open = '08:00:00'
time_close = '17:00:00'
times = []
for i in range(0, 24 * 4):
date = (d.combine(o_d.date.today(), time()) + timedelta(minutes=15) * i).time()
date_str = date.strftime("%H:%M:%S")
if time_open <= date_str <= time_close:
times.append(date_str)
#times.append(date)
print(times)
Edit from comments: If you wanted a tuple representation of what’s typically called “military” time vs what most people are used to i.e. – AM / PM you can do the following
from datetime import datetime as d, timedelta, time
import datetime as o_d
time_open = '08:00:00'
time_close = '17:00:00'
times = []
for i in range(0, 24 * 4):
date = (d.combine(o_d.date.today(), time()) + timedelta(minutes=15) * i).time()
date_str = date.strftime("%H:%M:%S")
if time_open <= date_str <= time_close:
#If you want a list of just standard times
#times.append(date_str)
#If you want the time representation(s) here and no longer need the
#the datetime object
times.append((date_str, date.strftime("%I:%M %p"))
#If you still need the datetime object append this do list comprehension below in comment with #options = ....
#times.append(date)
print(times)
#assuming you have appended date above see comment
#options = [( c.strftime("%H:%M:%S"), c.strftime("%I:%M %p")) for c in times]
👤Pythonista
Source:stackexchange.com