Python 如何检查两个日期之间的差异(以秒为单位)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4362491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:26:08  来源:igfitidea点击:

How do I check the difference, in seconds, between two dates?

pythontimedatediff

提问by Alex

There has to be an easier way to do this. I have objects that want to be refreshed every so often, so I want to record when they were created, check against the current timestamp, and refresh as necessary.

必须有一种更简单的方法来做到这一点。我有想要经常刷新的对象,所以我想记录它们的创建时间,检查当前时间戳,并根据需要刷新。

datetime.datetime has proven to be difficult, and I don't want to dive into the ctime library. Is there anything easier for this sort of thing?

datetime.datetime 已被证明很困难,我不想深入研究 ctime 库。这种事情有什么更容易的吗?

采纳答案by Rich Signell

if you want to compute differences between two known dates, use total_secondslike this:

如果要计算两个已知日期之间的差异,请total_seconds像这样使用:

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()

86400.0

86400.0

#note that seconds doesn't give you what you want:
(b-a).seconds

0

0

回答by matcheek

import time  
current = time.time()

...job...
end = time.time()
diff = end - current

would that work for you?

那对你有用吗?

回答by Jarret Hardie

>>> from datetime import datetime

>>>  a = datetime.now()

# wait a bit 
>>> b = datetime.now()

>>> d = b - a # yields a timedelta object
>>> d.seconds
7

(7 will be whatever amount of time you waited a bit above)

(7 将是您在上面等待的任何时间)

I find datetime.datetime to be fairly useful, so if there's a complicated or awkward scenario that you've encountered, please let us know.

我发现 datetime.datetime 非常有用,因此如果您遇到了复杂或尴尬的情况,请告诉我们。

EDIT: Thanks to @WoLpH for pointing out that one is not always necessarily looking to refresh so frequently that the datetimes will be close together. By accounting for the days in the delta, you can handle longer timestamp discrepancies:

编辑:感谢@WoLpH 指出人们不一定总是希望如此频繁地刷新以致日期时间会接近。通过考虑增量中的天数,您可以处理更长的时间戳差异:

>>> a = datetime(2010, 12, 5)
>>> b = datetime(2010, 12, 7)
>>> d = b - a
>>> d.seconds
0
>>> d.days
2
>>> d.seconds + d.days * 86400
172800

回答by pygaur

We have function total_seconds() with Python 2.7 Please see below code for python 2.6

我们有 Python 2.7 的函数 total_seconds() 请参见下面的 Python 2.6 代码

import datetime
import time  

def diffdates(d1, d2):
    #Date format: %Y-%m-%d %H:%M:%S
    return (time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S")) -
               time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S")))

d1 = datetime.now()
d2 = datetime.now() + timedelta(days=1)
diff = diffdates(d1, d2)

回答by therealadrain

Here's the one that is working for me.

这是对我有用的那个。

from datetime import datetime

date_format = "%H:%M:%S"

# You could also pass datetime.time object in this part and convert it to string.
time_start = str('09:00:00') 
time_end = str('18:00:00')

# Then get the difference here.    
diff = datetime.strptime(time_end, date_format) - datetime.strptime(time_start, date_format)

# Get the time in hours i.e. 9.60, 8.5
result = diff.seconds / 3600;

Hope this helps!

希望这可以帮助!