In python how do I check if its a weekday (Monday - Friday) and the time is between 10 AM to 3 PM?
now(). time() print(time_in_range(start, end, current)) # True (if you're not a night owl) ;) The code returns True if you run it between (hh:mm:ss) 00:00:00 and 23:55:00 on your computer. Only if you run it between 23:55:00 and 23:59:59 it returns False .
Check if a date is a weekday or weekend We can use the weekday() method of a datetime. date object to determine if the given date is a weekday or weekend. Note: The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.
Use the datetime. time class for start , end , and x . if start=today[10:00], end=tomorrow[10:00], and x = 11:00 then start <= x <= end = False !!! @PauloScardine: You have to choose how to interpret it: whether [10:00, 10:00] has length 0 or 24 hours.
>>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With