检查当前时间是两次之间

时间:2016-02-07 19:58:52

标签: date python-3.x python-3.4

我正在尝试检查当前时间是否在两个预先确定的时间之间以及当天是星期一,星期二,星期三或星期四。类似的东西:

if day = monday, tuesday, wednesday or thursday
   if current_time > 6:00 PM and current_time < 6:15 PM
      then do something

这是我到目前为止所剩下的,唯一剩下的就是设置我要检查的两次:

weekday = datetime.datetime.today().weekday()
hour = datetime.datetime.time(datetime.datetime.now())

if weekday < 5:
    if hour

1 个答案:

答案 0 :(得分:0)

您可以使用datetime.weekday()方法和属性:datetime.hourdatetime.minute

now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
    do_something()