Python Time conversion h:m:s to seconds

后端 未结 7 1198
青春惊慌失措
青春惊慌失措 2021-01-02 11:32

I am aware that with the timedelta function you can convert seconds to h:m:s using something like:

>> import datetime
>> str(datetime.timedelta(s         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 11:44

    >>> import time, datetime
    >>> a = time.strptime("00:11:06", "%H:%M:%S")
    >>> datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds
    666
    

    And here's a cheeky one liner if you're really intent on splitting over ":"

    >>> s = "00:11:06"
    >>> sum(int(i) * 60**index for index, i in enumerate(s.split(":")[::-1]))
    666
    

提交回复
热议问题