本文实例讲述了python 3.3实现计算两个日期间隔秒数/天数的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> import datetime >>> d1 = datetime.datetime.now() >>> d2 = datetime.datetime.now() >>> interval = d2 - d1 >>> interval datetime.timedelta( 0 , 14 , 670206 ) >>> sec = interval.days * 24 * 3600 + interval.seconds >>> sec 14 >>> total_sec = interval.total_seconds() >>> total_sec 14.670206 >>> |
运行结果截图:
另外一种,指定日期。
1
2
3
4
5
6
7
8
9
|
>>> d1 = datetime.datetime( 2016 , 8 , 10 , 10 , 20 , 30 ) >>> d2 = datetime.datetime( 2016 , 8 , 10 , 10 , 30 , 30 ) >>> interval = d2 - d1 >>> interval datetime.timedelta( 0 , 600 ) >>> sec = interval.days * 24 * 3600 + interval.seconds >>> sec 600 >>> |
运行结果截图:
间隔天数
1
2
3
4
5
6
7
8
9
|
>>> import time >>> start_date = '2017-06-01' >>> end_date = '2017-06-05' >>> start_sec = time.mktime(time.strptime(start_date, '%y-%m-%d' )) >>> end_sec = time.mktime(time.strptime(end_date, '%y-%m-%d' )) >>> work_days = int ((end_sec - start_sec) / ( 24 * 60 * 60 )) >>> work_days 4 >>> |
运行结果截图:
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/uvyoaa/article/details/52170212