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

前端 未结 12 1763
陌清茗
陌清茗 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:41

    def get_sec(time_str):
        """Get Seconds from time."""
        h, m, s = time_str.split(':')
        return int(h) * 3600 + int(m) * 60 + int(s)
    
    
    print(get_sec('1:23:45'))
    print(get_sec('0:04:15'))
    print(get_sec('0:00:25'))
    

提交回复
热议问题