jdecker jdecker - 3 months ago 9x
Python Question

Python determine if time is between two hours in variables

settingsNightStart = settings[28]
settingsNightStop = settings[29]

if datetime.time(settingsNightStart) <= datetime.time.now() <= datetime.time(settingsNightStop):
Night = 1

I have two variables coming from a database that users store there starting time and ending time in 24 hr format (0-23). Is there a better way to store this setting from the user?

I need to figure out how determine if the current time is between those two times. Most of the time it's going to need to be determined over night where the users time will be starting at 8PM and ending at 7AM. When I run the script I need to know if the current time is between those hours. I don't know if datetime can work with what I'm trying to do?

Thanks for any help.


suppose you have start_hour and stop_hour:

this_hour = datetime.now().hour if start_hour > stop_hour: night = start_hour < this_hour or this_hour < stop_hour else: night = start_hour < this_hour < stop_hour