我的代码有问题。我试图减去两次,但它给了我一个错误:
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'
错误是跳到这一行:
diff = (end_dt - start_dt)
当我尝试这个时:
start = "09:35:23"
end = "10:23:00"
start_dt = time.strptime(start, '%H:%M:%S')
end_dt = time.strptime(end, '%H:%M:%S')
diff = (end_dt - start_dt)
你能帮我解决一下如何解决我得到的错误吗?
答案 0 :(得分:3)
您需要使用datetime
模块:
import datetime
start = "09:35:23"
end = "10:23:00"
start_dt = datetime.datetime.strptime(start, '%H:%M:%S')
end_dt = datetime.datetime.strptime(end, '%H:%M:%S')
diff = (end_dt - start_dt)
print(diff)
<强>输出强>
datetime.timedelta(0, 2857)
这会生成两个datetime
个对象,start_dt
和end_dt
。当您从另一个中减去一个时,它会返回timedelta
。