Get current time in milliseconds in Python?
Using time.time()
:
import time
def current_milli_time():
return round(time.time() * 1000)
Then:
>>> current_milli_time()
1378761833768
time.time()
may only give resolution to the second, the preferred approach for milliseconds is datetime
.
from datetime import datetime
dt = datetime.now()
dt.microsecond
From version 3.7 you can use time.time_ns()
to get time as passed nano seconds from epoch.
So you can do
ms = time.time_ns() // 1_000_000
to get time in mili-seconds as integer.