12

How to calculate milliseconds,from the code below.

a = datetime.datetime.now()
b = datetime.datetime.now()
c = b - a

>>> c

>>> c.days
0
>>> c.seconds
4
>>> c.microseconds
Nakilon
  • 34,866
  • 14
  • 107
  • 142
Hulk
  • 32,860
  • 62
  • 144
  • 215
  • 1
    related: [Get current time in milliseconds in Python?](http://stackoverflow.com/q/5998245/4279) – jfs Mar 24 '15 at 20:18

2 Answers2

15
milliseconds = (c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
John Machin
  • 81,303
  • 11
  • 141
  • 189
14

Or, new since 2.7:

c.total_seconds()*1000

(https://docs.python.org/2/library/datetime.html)

zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56