views:

105

answers:

2

In python how do I check if its a weekday (Monday - Friday) and the time is between 10 AM to 3 PM?

A: 
>>> 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

SilentGhost
+3  A: 
>>> 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
The MYYN
`range(6)` has length 6
SilentGhost
thx, should be range(1, 6) ..
The MYYN
The hour check should be `d.hour in range(10, 15)`. If the hour equals 15, it's past 3 PM, so we shouldn't include 15 in the range of allowed hours.
Pär Wieslander
How do we go if the time range is 9:30 AM to 3:30 PM?
Vishal
Ive updated the answer to show how to check minute as well.
mizipzor
For checking in range 9.30AM to 3.30PM: d.hour*60+d.minute in range(9*60+30, 15*60+30)
invariant