How to Get Current Time in Milliseconds in Python

Created
Modified

Using time Method

The time.time() method returns the time in seconds since the epoch as a floating point number. For example,

#!/usr/bin/python3

# Import module
import time

ms = round(time.time() * 1000)
print(ms)
1652865940186

Using time_ns Method

The time.time_ns() method returns time as an integer number of nanoseconds since the epoch.

The following example should cover whatever you are trying to do:

#!/usr/bin/python3

# Import module
import time

ms = time.time_ns()
print(ms)
1652866090096069080

Using microsecond Attribute

For example,

#!/usr/bin/python3

# Import module
import datetime

t = datetime.datetime.now()
ms = t.microsecond
print(ms)
452195

Related Tags

#get# #time#