Search code examples
pythondatetimestrptimemilliseconds

How to convert mm:ss.ms to ss.ms?


I'm trying to make a conversion for below example:

original time: 1:03.091
converted time: 63.09

I did some research and found that I can add up the min to secs, but don't know how to add the milliseconds anymore. Below is what I've managed to do so far:

a = "01:40.44"
x = time.strptime(a,'%M:%S.%f')                                              
datetime.timedelta(minutes=x.tm_min,seconds=x.tm_sec).total_seconds()

100.0

In this case, how could I get 100.44, for example?


Solution

  • You can use datetime instead of time. For example:

    >>from datetime import datetime
    >>x = datetime.strptime(a,'%M:%S.%f')
    1900-01-01 00:01:40.437000
    >>x.microsecond
    437000
    

    Edit: You can get anything hour, min, second and sum it up.

    from datetime import datetime

    a = "01:40.437"
    x = datetime.strptime(a,'%M:%S.%f')
    time = x.minute*60+x.second+x.microsecond/1000000
    
    >>time
    100.437