James James - 4 months ago 12
Python Question

How do I determine if current server time isn't within a specified range using Python ?

I need to check in Python if the current time on the server isn't in the time range of 22:00PM - 04:00AM.

What is the correct way to write this code ?

Thanks !

Answer

Try this below:

import time
def isWithinRange():
    hour = time.localtime(time.time()).tm_hour
    return hour >= 22 or hour <= 4
Comments