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.
-
4I'm not sure about putting this as a answer, but I put it here as comment: surprisingly, I found that Python accepts comparisons like `'23:23:00' > '23:59:00'` or `'23:23:00' > '23:09:22'`, just as you would do between two `datetime.datetime.now().strftime('%H:%M')` values. Is it the best way of achieving comparison between times? I don't know. Does it work? As far as I tested, it worked fine. But definitely (and of course): not a chance of using this with datetime string that have month, weekdays. – ivanleoncz Jul 03 '20 at 18:10
8 Answers
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
-
9You might want to flip the acceptance to Pär Wieslander's answer (and generally should wait a few more minutes than you did :P), as it's a bit more specific to exactly what you asked. – Dec 02 '09 at 08:40
-
I'm also not sure the premise of this answer is correct. Datetime has built in functions to reduce a specific point in time to it's daily recurring digits. – Jesse Reza Khorasanee Jan 21 '21 at 01:13
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

- 28,374
- 7
- 55
- 54
-
3Make sure to do `datetime.datetime.now().time()`, don't forget the brackets in `now()`! – Jose Salvatierra Aug 13 '13 at 09:49
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
>>>

- 38,306
- 16
- 108
- 142
Surprised I haven't seen this one liner here:
datetime.datetime.now().hour == 8

- 3,140
- 4
- 36
- 53
You Can Use Timedelta fuction for x time increase comparision.
>>> import datetime
>>> now = datetime.datetime.now()
>>> after_10_min = now + datetime.timedelta(minutes = 10)
>>> now > after_10_min
False

- 91
- 2
- 6
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"

- 162
- 1
- 8
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.

- 7,052
- 3
- 40
- 48
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

- 41,928
- 25
- 127
- 172