0

I want to subtract two dates and convert the results to milliseconds, I can subtract two dates but not sure how to convert to milliseconds, for example the final output of below code is '0:11:27.581293', I want to convert this to unit in milliseconds, example 12400ms like that, please help.

>>> import dateutil.parser as dparser
>>> stime='2019-04-23 04:22:50.421406'
>>> etime='2019-04-23 04:34:18.002699'
>>> str((dparser.parse(etime, fuzzy=True) - dparser.parse(stime, fuzzy=True)))
'0:11:27.581293'

Expected results: convert '0:11:27.581293' to milliseconds.

Rafi Shiek
  • 53
  • 1
  • 5
  • Does this answer your question? [Understanding timedelta](https://stackoverflow.com/questions/6749294/understanding-timedelta) – Anthony Kong May 05 '20 at 11:45

2 Answers2

1

Use total_seconds() * 1000

Ex:

import dateutil.parser as dparser
stime='2019-04-23 04:22:50.421406'
etime='2019-04-23 04:34:18.002699'
print((dparser.parse(etime, fuzzy=True) - dparser.parse(stime, fuzzy=True)).total_seconds() * 1000)
#or 
print(int((dparser.parse(etime, fuzzy=True) - dparser.parse(stime, fuzzy=True)).total_seconds()* 1000))

Output:

687581.293
687581
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Use the code below which returns the difference in time in microseconds. Divide by 1000 to get to milliseconds.

diff = dparser.parse(etime, fuzzy=True) - dparser.parse(stime, fuzzy=True)
print(diff.microseconds)
Srikant
  • 294
  • 1
  • 2
  • 3