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
  • 33,683
  • 14
  • 104
  • 137
Hulk
  • 30,904
  • 60
  • 142
  • 212
  • 1
    related: [Get current time in milliseconds in Python?](http://stackoverflow.com/q/5998245/4279) – jfs Mar 24 '15 at 20:18

2 Answers2

14
milliseconds = (c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0
SilentGhost
  • 287,765
  • 61
  • 300
  • 288
John Machin
  • 78,552
  • 11
  • 135
  • 182
13

Or, new since 2.7:

c.total_seconds()*1000

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

zeeMonkeez
  • 4,939
  • 3
  • 28
  • 51