如何在Python中比较时间?

问题:如何在Python中比较时间?

我看到可以进行日期比较,也可以进行日期比较datetime.timedelta(),但是我正在努力寻找如何检查当前时间(datetime.datetime.now())是否早于指定时间(晚于或晚于指定时间)(例如,上午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.


回答 0

无法将特定时间点(例如“现在”)与一个固定的重复事件(每天早上8点发生)进行比较。

您可以检查现在是在今天上午8点之前还是之后:

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

您可以使用对象的time()方法datetime来获取一天中的时间,您可以将其用于比较而无需考虑日期:

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

您可以直接比较datetime.datetime对象

例如:

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

受到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"

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

另一种不添加依赖项或不使用datetime的方法是对time对象的属性进行一些数学运算。它具有小时,分钟,秒,毫秒和时区。对于非常简单的比较,小时和分钟应该足够了。

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

除非您有一个非常简单的用例,否则我不建议您这样做。对于需要时区感知或日期感知的任何事物,都应使用datetime。

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

您可以将Timedelta函数用于x时间增加比较。

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