I want to do a time minus: 24Hours - 14Hours 20 minutes 10 seconds,write a function:
def time_minus(start,end):
def convert_sec(time):
hours = int(time.split(":")[0])
mins = int(time.split(":")[1])
secs = int(time.split(":")[2])
return hours*60*60 + mins*60 + secs
start_time = convert_sec(start)
end_time = convert_sec(end)
st = end_time-start_time
hours = str(st//3600).rjust(2,'0')
mins = str((st%3600)//60).rjust(2,'0')
secs = str((st%3600)%60).rjust(2,'0')
result = hours + " hours " + mins + " minutes " + secs + " seconds"
print(result)
return hours + ":" + mins + ":" + secs
The output:
x=time_minus("14:20:10","24:00:00")
09 hours 39 minutes 50 seconds
x
'09:39:50'
Is there a some python's lib to do the minus?How can call it?
>>> from datetime import datetime
>>> s1 = '14:20:10'
>>> s2 = '24:00:00'
>>> FMT = '%H:%M:%S'
>>> tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.9/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '24:00:00' does not match format '%H:%M:%S'
I have to customize a function because of time data '24:00:00' does not match format '%H:%M:%S'
!!!