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

问题描述:

必须有一种更简单的方法来执行此操作。我有想要经常刷新的对象,因此我想记录它们的创建时间,对照当前时间戳进行检查,并根据需要刷新。

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已证明很困难,我不想进入ctime库。这样的事情有什么容易的吗?

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?

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

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

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

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

0