python 时间差计算
生活随笔
收集整理的這篇文章主要介紹了
python 时间差计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例如:時間格式是
2020-01-29?16:31:31和2025-01-23?15:30:30,怎么求得兩個時間相減的時間差? 具體如下: import?time?as?t import?datetime?as?d #定義時間差函數 def?myDate(date1,?date2):date1?=?t.strptime(date1,?"%Y-%m-%d?%H:%M:%S")date2?=?t.strptime(date2,?"%Y-%m-%d?%H:%M:%S")startTime?=?t.strftime("%Y-%m-%d?%H:%M:%S",?date1)endTime?=?t.strftime("%Y-%m-%d?%H:%M:%S",?date2)startTime?=?d.datetime.strptime(startTime,"%Y-%m-%d?%H:%M:%S")endTime?=?d.datetime.strptime(endTime,"%Y-%m-%d?%H:%M:%S")date?=?endTime-?startTimereturn?date#參數賦值 date1?=?"2020-01-29?16:31:31" date2?=?"2025-01-23?15:30:30" #獲得時間差秒數 seconds?=?myDate(date1,?date2).seconds #獲得時間差 date?=?myDate(date1,?date2) print(seconds) print(date)轉載于:https://blog.51cto.com/lynnpaul/1891429
總結
以上是生活随笔為你收集整理的python 时间差计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python练习-四舍五入
- 下一篇: CSS3的边框(二)