国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

python怎么計算時間差

極客小將2021-03-02-
簡介python計算時間差的方法:首先引入datetime包;然后通過“(time_2_struct-time_1_struct)”方式計算出同一天情形下的時間差或者不同天的時間差即可。本文操作環境:windows7系統、python2.7.14版,DELLG3電腦。python求時間差python求時

python計算時間差的方法:首先引入datetime包;然后通過“(time_2_struct - time_1_struct)”方式計算出同一天情形下的時間差或者不同天的時間差即可。43m少兒編程網-https://www.pxcodes.com

43m少兒編程網-https://www.pxcodes.com

本文操作環境:windows7系統、python2.7.14版,DELL G3電腦。43m少兒編程網-https://www.pxcodes.com

python求時間差43m少兒編程網-https://www.pxcodes.com

python求時間差主要是用的datetime包,包括同一天情形下的時間差和不同天情形下的時間差。43m少兒編程網-https://www.pxcodes.com

from datetime import datetime, date

1. 同一天情形下的時間差(秒)seconds ,分鐘由秒數除以60即可43m少兒編程網-https://www.pxcodes.com

#計算時間差的分鐘數 # 同一天的時間差 time_1 = '2020-03-02 15:00:00' time_2 = '2020-03-02 16:00:00' time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S") time_2_struct = datetime.strptime(time_2, "%Y-%m-%d %H:%M:%S") seconds = (time_2_struct - time_1_struct).seconds print('同一天的秒數為:') print(seconds)

43m少兒編程網-https://www.pxcodes.com

2. 不同天情形下的時間差(也可計算同一天情形下的時間差),total_seconds43m少兒編程網-https://www.pxcodes.com

# 不同天的時間差 time_1 = '2020-03-02 15:00:00' time_2 = '2020-03-03 16:00:00' time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S") time_2_struct = datetime.strptime(time_2, "%Y-%m-%d %H:%M:%S") # 來獲取時間差中的秒數。注意,seconds獲得的秒只是時間差中的小時、分鐘和秒部分,沒有包含天數差,total_seconds包含天數差 # 所以total_seconds兩種情況都是可以用的 total_seconds = (time_2_struct - time_1_struct).total_seconds() print('不同天的秒數為:') print(int(total_seconds)) min_sub = total_seconds / 60 print('不同天的分鐘數為:') print(int(min_sub))

43m少兒編程網-https://www.pxcodes.com

【推薦學習:python視頻教程】43m少兒編程網-https://www.pxcodes.com

3. 只有時間time沒有日期時,求時間差先可以加上一個相同的日期,再求時間差,datetime.combine 方法43m少兒編程網-https://www.pxcodes.com

# 只有時間time沒有日期時,求時間差先可以加上一個相同的日期,再求時間差 # date.min能表示的**小日期 # date.max能表示的**大日期 # date.today()返回一個當前日期對象 # datetime.combine:根據所給的date和time創建一個datetime對象 time_sub = datetime.combine(date.min, time_2_struct.time()) - datetime.combine(date.min, time_1_struct.time()) print('----- 與**小日期結合: ------') print(time_sub.seconds/60) time_sub = datetime.combine(date.today(), time_2_struct.time()) - datetime.combine(date.today(), time_1_struct.time()) print('----- 與當天日期結合: ------') print(time_sub.seconds/60) print(time_sub.total_seconds()/60)

以上就是python怎么計算時間差的詳細內容,更多請關注少兒編程網其它相關文章!43m少兒編程網-https://www.pxcodes.com

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 密云县| 封丘县| 洪江市| 德保县| 义乌市| 凤阳县| 墨脱县| 中卫市| 新竹市| 呼图壁县| 安岳县| 安新县| 瓦房店市| 阳城县| 遵化市| 友谊县| 桦川县| 扶沟县| 迭部县| 观塘区| 莱芜市| 阳城县| 陆良县| 清涧县| 易门县| 永嘉县| 社会| 莱西市| 确山县| 宣化县| 彰化市| 波密县| 象州县| 咸丰县| 繁昌县| 响水县| 六枝特区| 山东省| 浏阳市| 冕宁县| 兴化市|