How to convert an H:MM:SS time string to seconds in Python?

前端 未结 12 1748
陌清茗
陌清茗 2020-11-29 22:14

Basically I have the inverse of this problem: Python Time Seconds to h:m:s

I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I nee

12条回答
  •  遥遥无期
    2020-11-29 22:45

    You can split the time into a list and add each individual time component, multiplying the hours component by 3600 (the number of seconds in an hour) and the minutes component by 60 (number of seconds in a minute), like:

    timeInterval ='00:35:01'
    list = timeInterval.split(':')
    hours = list[0]
    minutes = list[1]
    seconds = list[2]
    total = (int(hours) * 3600 + int(minutes) * 60 + int(seconds))
    print("total = ", total)
    

提交回复
热议问题