## 问题：如何在Python中比较时间？

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.

## 回答 0

``````>>> 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’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
``````

## 回答 1

``````>>> 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``````

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
``````

## 回答 2

``````>>> 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 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
>>>
``````

## 回答 3

``````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"``````

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"
``````

## 回答 4

``````d = datetime.utcnow()
t = d.time()
print t.hour,t.minute,t.second``````

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.

## 回答 5

``````>>> import datetime
>>> import time
>>> a =  datetime.datetime.now()
>>> time.sleep(2.0)
>>> b =  datetime.datetime.now()
>>> print a < b
True
>>> print a == b
False``````

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
``````

## 回答 6

``datetime.datetime.now().hour == 8``

Surprised I haven’t seen this one liner here:

``````datetime.datetime.now().hour == 8
``````

## 回答 7

``````>>> import datetime

>>> now = datetime.datetime.now()
>>> after_10_min = now + datetime.timedelta(minutes = 10)
>>> now > after_10_min

False``````

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
``````

Just A combination of these answers this And Roger