In python how do I check if its a weekday (Monday - Friday) and the time is between 10 AM to 3 PM?
https://docs.python.org/2/library/datetime.html
The documentation states that date.weekday()
returns the day of the week as an integer, where Monday is 0 and Sunday is 6. So this code will work:
import datetime
# Hours must be 24 hour clock times
def is_weekday_and_time_in_range(date, start_hour, end_hour):
return date.weekday() < 5 and date.hour in range(start_hour, end_hour)
today = datetime.datetime.now()
is_weekday_and_time_in_range(today, 10, 15)
>>> import datetime
>>> d = datetime.datetime.now()
# => datetime.datetime(2009, 12, 15, 13, 50, 35, 833175)
# check if weekday is 1..5
>>> d.isoweekday() in range(1, 6)
True
# check if hour is 10..15
>>> d.hour in range(10, 15)
True
# check if minute is 30
>>> d.minute==30
False
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 12, 15, 12, 45, 33, 781000)
>>> now.isoweekday()
2 # Tuesday
time between 10 a.m. and 3 p.m. is right there as well