我的代码有问题。我试图减去两次,但它给了我一个错误:
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'
Run Code Online (Sandbox Code Playgroud)
错误在这条线上跳跃:
diff = (end_dt - start_dt)
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时:
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)
Run Code Online (Sandbox Code Playgroud)
你能帮我解决我遇到的错误吗?
您需要使用该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)
Run Code Online (Sandbox Code Playgroud)
输出
datetime.timedelta(0, 2857)
Run Code Online (Sandbox Code Playgroud)
这将生成两个datetime对象,start_dt和end_dt。当你从另一个中减去一个时,它返回一个timedelta。
| 归档时间: |
|
| 查看次数: |
6599 次 |
| 最近记录: |