2

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Penny
  • 1,218
  • 1
  • 13
  • 32
  • [Formatting posts](https://stackoverflow.com/help/formatting) ... [Formatting help](https://stackoverflow.com/editing-help) – wwii May 19 '18 at 01:19
  • [`time.struct_time`](https://docs.python.org/3/library/time.html#time.struct_time) instances don't look like they have fractions of seconds. But `datetime.dateime` objects do. – wwii May 19 '18 at 01:27

1 Answers1

2

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
NLag
  • 75
  • 6
  • Hi @NLag, thanks but this is not what the intended output though..In this case, I'll need `100.44`, not getting microseconds.. – Penny May 19 '18 at 01:29
  • I edited the answer you need, using the `datetime` you can extract other part just like with `time` – NLag May 19 '18 at 01:32