Get current time in milliseconds in Python? Get current time in milliseconds in Python? python python

Get current time in milliseconds in Python?


Using time.time():

import timedef 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 datetimedt = 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.