Python 中 datetime 和 second 之间的转换

计算两个 datetime 之间的秒数

1
2
3
4
5
6
7
import datetime
a = datetime.datetime(2013,12,30,23,59,59)
b = datetime.datetime(2013,12,31,23,59,59)
print (b-a).total_seconds()
86400.0

将秒数转为 day hour:minute:second

1
2
3
4
5
>>> import datetime
>>> print str(datetime.timedelta(seconds=3661))
1:01:01
>>> print str(datetime.timedelta(seconds=86401))
1 day, 0:00:01

参考资料