简体   繁体   English

在Python中找到两个时间字符串之间的差异

[英]Find difference between two time strings in Python

I have two times in the string format HHMM and I want to find the difference in minutes. 我有两次字符串格式HHMM,我想在几分钟内找到区别。

I've tried the below but I'm getting the following error: 我尝试了以下操作,但出现以下错误:

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' 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))

Output: 输出:

3600.0
60.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM