In python how do I check if its a weekday (Monday - Friday) and the time is between 10 AM to 3 PM?
views:
105answers:
2
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
2009-12-15 12:48:17
+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
2009-12-15 12:52:36
`range(6)` has length 6
SilentGhost
2009-12-15 12:55:23
thx, should be range(1, 6) ..
The MYYN
2009-12-15 12:57:03
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
2009-12-15 13:00:58
How do we go if the time range is 9:30 AM to 3:30 PM?
Vishal
2009-12-15 13:22:40
Ive updated the answer to show how to check minute as well.
mizipzor
2009-12-15 14:15:02
For checking in range 9.30AM to 3.30PM: d.hour*60+d.minute in range(9*60+30, 15*60+30)
invariant
2009-12-15 14:25:21