I have data of experiments with time greater than 24 hours. For ex. [23:24:44, 25:10:44]
. To operate duration of tests, I like to use Python, however I have a value error when I create datetime.time() with hours more than 23:59:.
Asked
Active
Viewed 323 times
-1

joe kid
- 33
- 4
-
2What exact error? What code triggers it? Please show a [mcve] – Mad Physicist Aug 24 '21 at 14:04
1 Answers
2
You could split your time by the colons in order to get a list of the component parts, which you could then use to initialise your timedelta:
from datetime import timedelta
myDuration = "25:43:12"
mD = [int(x) for x in myDuration.split(":")]
delta = timedelta(hours=mD[0], minutes=mD[1], seconds=mD[2])
print(delta)
# 1 day, 1:43:12

Ari Cooper-Davis
- 3,374
- 3
- 26
- 43
-
To clarify for other readers. My goal is to compute time duration. I was confused and used datetime.time(hour=25, minute=43, second=12) which gives ValueError: hour must be in 0..23. However, datetime.time() is not a duration, datetime.timedelta() is the correct function for this case. – joe kid Aug 26 '21 at 13:11