在python中经常会用到计算两个时间差,两个日期类型进行相减可以获取到时间差。
经常会使用seconds来获取,其实seconds获取的是仅仅是时间差的秒数,忽略微秒数,忽略天数。
total_seconds()是获取两个时间之间的总差。
total_seconds() = (td.microseconds+ (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 # td是timedelta对象
import datetime
t1 = datetime.datetime.strptime("2017-9-06 10:30:00", "%Y-%m-%d %H:%M:%S")
t2 = datetime.datetime.strptime("2017-9-06 12:30:00", "%Y-%m-%d %H:%M:%S")
interval_time = (t2 - t1).seconds # 输入的结果:7200
total_interval_time = (t2 - t1).total_seconds() # 输出结果是: 7200.0
print interval_time
print total_interval_time
# 换一个方式
t1 = datetime.datetime.strptime("2017-9-06 10:30:00", "%Y-%m-%d %H:%M:%S")
t2 = datetime.datetime.strptime("2017-9-08 12:30:00", "%Y-%m-%d %H:%M:%S")
interval_time = (t2 - t1).seconds # 输入的结果:7200
total_interval_time = (t2 - t1).total_seconds() # 输出结果是: 180000.0
print interval_time
print total_interval_time
td = (t2 - t1)
print((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6)
# datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
td = datetime.timedelta(6, 5, 1, 800, 12, 3)
print td # 6 days, 3:12:05.800001
print td.seconds # 11525 忽略微秒和天
print td.total_seconds() # 529925.800001
参考:
https://docs.python.org/2/library/datetime.html?#timedelta-objects