簡體   English   中英

在Python中找到兩個時間字符串之間的差異

[英]Find difference between two time strings in Python

我有兩次字符串格式HHMM,我想在幾分鍾內找到區別。

我嘗試了以下操作,但出現以下錯誤:

TypeError:-:“ datetime.time”和“ datetime.time”的不受支持的操作數類型

import datetime  

a = "0628"
b = "0728"

aSep = a[:2] + ':' + a[2:]
bSep = b[:2] + ':' + b[2:]

timeA = datetime.datetime.strptime(aSep, '%H:%M').time()
timeB = datetime.datetime.strptime(bSep, '%H:%M').time()

diff = timeB -timeA
print diff
import datetime  

a = "0628"
b = "0728"

timeA  = datetime.datetime.strptime(a, "%H%M")
timeB  = datetime.datetime.strptime(b, "%H%M")

print((timeB-timeA).total_seconds())
print(((timeB-timeA).total_seconds()/60.0))

輸出:

3600.0
60.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM