在Python

时间:2018-04-27 09:55:53

标签: python datetime time

我有两次字符串格式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

1 个答案:

答案 0 :(得分:2)

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