In Python check if current time is less than specific time?
18,022
Solution 1
The datetime
module should be very helpful for you. Try something like the following:
>>> d = datetime.datetime.utcnow()
>>> print d
2015-06-17 11:39:48.585000
>>> d.hour
11
>>> if d.hour < 9:
print "Run your code here"
# nothing happens, it's after 9:00 here.
>>>
Solution 2
Did you try this
In all computers convert time to UTC and then compare it with the time you want to start
from datetime import datetime
now_UTC = datetime.utcnow() # Get the UTC time
# check for the condition
if(now_UTC.hour < 9):
do something()
Comments
-
Mo. almost 2 years
In a Python script I want it to check before executing if it's before 9AM UTC so that it can do something specific.
I was wondering what the best way to do this is in terms of checking the time to ensure it's before 9AM each day the script is run? Keeping in mind that the code could be running on different machines that have different timezones.
Thanks