How to compare times of the day?
Question:
I see that date comparisons can be done and there’s also datetime.timedelta()
, but I’m struggling to find out how to check if the current time (datetime.datetime.now()
) is earlier, later or the same than a specified time (e.g. 8am) regardless of the date.
Answers:
datetime have comparison capability
>>> import datetime
>>> import time
>>> a = datetime.datetime.now()
>>> time.sleep(2.0)
>>> b = datetime.datetime.now()
>>> print a < b
True
>>> print a == b
False
You can compare datetime.datetime objects directly
E.g:
>>> a
datetime.datetime(2009, 12, 2, 10, 24, 34, 198130)
>>> b
datetime.datetime(2009, 12, 2, 10, 24, 36, 910128)
>>> a < b
True
>>> a > b
False
>>> a == a
True
>>> b == b
True
>>>
You can’t compare a specific point in time (such as “right now”) against an unfixed, recurring event (8am happens every day).
You can check if now is before or after today’s 8am:
>>> import datetime
>>> now = datetime.datetime.now()
>>> today8am = now.replace(hour=8, minute=0, second=0, microsecond=0)
>>> now < today8am
True
>>> now == today8am
False
>>> now > today8am
False
You can use the time()
method of datetime
objects to get the time of day, which you can use for comparison without taking the date into account:
>>> this_morning = datetime.datetime(2009, 12, 2, 9, 30)
>>> last_night = datetime.datetime(2009, 12, 1, 20, 0)
>>> this_morning.time() < last_night.time()
True
Inspired by Roger Pate:
import datetime
def todayAt (hr, min=0, sec=0, micros=0):
now = datetime.datetime.now()
return now.replace(hour=hr, minute=min, second=sec, microsecond=micros)
# Usage demo1:
print todayAt (17), todayAt (17, 15)
# Usage demo2:
timeNow = datetime.datetime.now()
if timeNow < todayAt (13):
print "Too Early"
Another way to do this without adding dependencies or using datetime is to simply do some math on the attributes of the time object. It has hours, minutes, seconds, milliseconds, and a timezone. For very simple comparisons, hours and minutes should be sufficient.
d = datetime.utcnow()
t = d.time()
print t.hour,t.minute,t.second
I don’t recommend doing this unless you have an incredibly simple use-case. For anything requiring timezone awareness or awareness of dates, you should be using datetime.
Surprised I haven’t seen this one liner here:
datetime.datetime.now().hour == 8
I see that date comparisons can be done and there’s also datetime.timedelta()
, but I’m struggling to find out how to check if the current time (datetime.datetime.now()
) is earlier, later or the same than a specified time (e.g. 8am) regardless of the date.
datetime have comparison capability
>>> import datetime
>>> import time
>>> a = datetime.datetime.now()
>>> time.sleep(2.0)
>>> b = datetime.datetime.now()
>>> print a < b
True
>>> print a == b
False
You can compare datetime.datetime objects directly
E.g:
>>> a
datetime.datetime(2009, 12, 2, 10, 24, 34, 198130)
>>> b
datetime.datetime(2009, 12, 2, 10, 24, 36, 910128)
>>> a < b
True
>>> a > b
False
>>> a == a
True
>>> b == b
True
>>>
You can’t compare a specific point in time (such as “right now”) against an unfixed, recurring event (8am happens every day).
You can check if now is before or after today’s 8am:
>>> import datetime
>>> now = datetime.datetime.now()
>>> today8am = now.replace(hour=8, minute=0, second=0, microsecond=0)
>>> now < today8am
True
>>> now == today8am
False
>>> now > today8am
False
You can use the time()
method of datetime
objects to get the time of day, which you can use for comparison without taking the date into account:
>>> this_morning = datetime.datetime(2009, 12, 2, 9, 30)
>>> last_night = datetime.datetime(2009, 12, 1, 20, 0)
>>> this_morning.time() < last_night.time()
True
Inspired by Roger Pate:
import datetime
def todayAt (hr, min=0, sec=0, micros=0):
now = datetime.datetime.now()
return now.replace(hour=hr, minute=min, second=sec, microsecond=micros)
# Usage demo1:
print todayAt (17), todayAt (17, 15)
# Usage demo2:
timeNow = datetime.datetime.now()
if timeNow < todayAt (13):
print "Too Early"
Another way to do this without adding dependencies or using datetime is to simply do some math on the attributes of the time object. It has hours, minutes, seconds, milliseconds, and a timezone. For very simple comparisons, hours and minutes should be sufficient.
d = datetime.utcnow()
t = d.time()
print t.hour,t.minute,t.second
I don’t recommend doing this unless you have an incredibly simple use-case. For anything requiring timezone awareness or awareness of dates, you should be using datetime.
Surprised I haven’t seen this one liner here:
datetime.datetime.now().hour == 8