I have a problem with my code. I am trying to subtracting the two times but it give me an error:
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'
The error are jumping on this line:
diff = (end_dt - start_dt)
When I try this:
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)
Can you please help me how to fix the error that I am getting?
You need to use the datetime
module:
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)
Output
datetime.timedelta(0, 2857)
This generates two datetime
objects, start_dt
and end_dt
. When you subtract one from the other it returns a timedelta
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.