How do I check if it's Monday to Friday and the time is between 10 AM to 3 PM?

后端 未结 3 1712
無奈伤痛
無奈伤痛 2020-12-17 09:41

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

相关标签:
3条回答
  • 2020-12-17 10:21

    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)

    0 讨论(0)
  • 2020-12-17 10:22
    >>> 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
    
    0 讨论(0)
  • 2020-12-17 10:32
    >>> 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

    0 讨论(0)
提交回复
热议问题