How to convert a time string to seconds?

前端 未结 9 1859
轮回少年
轮回少年 2020-11-30 00:44

I need to convert time value strings given in the following format to seconds, for example:

1.\'00:00:00,000\' -> 0 seconds

2.\'00:00:10,000\' -> 10 s         


        
9条回答
  •  北海茫月
    2020-11-30 00:51

    There is always parsing by hand

    >>> import re
    >>> ts = ['00:00:00,000', '00:00:10,000', '00:01:04,000', '01:01:09,000']
    >>> for t in ts:
    ...     times = map(int, re.split(r"[:,]", t))
    ...     print t, times[0]*3600+times[1]*60+times[2]+times[3]/1000.
    ... 
    00:00:00,000 0.0
    00:00:10,000 10.0
    00:01:04,000 64.0
    01:01:09,000 3669.0
    >>> 
    

提交回复
热议问题