如何分辨两个datetime物体之间的时间差(以分钟为单位)?
>>> import datetime >>> first_time = datetime.datetime.now() >>> later_time = datetime.datetime.now() >>> difference = later_time - first_time >>> seconds_in_day = 24 * 60 * 60 datetime.timedelta(0, 8, 562000) >>> divmod(difference.days * seconds_in_day + difference.seconds, 60) (0, 8) # 0 minutes, 8 seconds
从第一次减去以后的时间将difference = later_time - first_time创建一个仅保留时差的datetime对象。在上面的示例中,它是0分钟,8秒和562000微秒。
difference = later_time - first_time
datetime