Does anyone know where I can find a good function that converts a string like "15 mins" or "1hr 20mins" into an integer denoting the number of seconds?
Asked
Active
Viewed 86 times
0
-
Does this answer your question? [How to construct a timedelta object from a simple string](https://stackoverflow.com/questions/4628122/how-to-construct-a-timedelta-object-from-a-simple-string) – wjandrea Nov 23 '19 at 16:28
-
1Welcome to Stack Overflow! Check out the [tour]. – wjandrea Nov 23 '19 at 16:29
1 Answers
1
I think you can find your solution here , and you can write your own function which does this, by writing a formal way to parse your time, you can transform your string into a datetime and the rest is straight forward.
from datetime import datetime, timedelta
# we specify the input and the format...
t = datetime.strptime("05:20:25","%H:%M:%S")
# ...and use datetime's hour, min and sec properties to build a timedelta
delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)
print(delta)
assert(5*60*60+20*60+25 == delta.total_seconds())

Safiy Zaghbane
- 38
- 7