HomeLanguagesGet current time in milliseconds using Python

Get current time in milliseconds using Python

In this article, we will get the time in milliseconds using Python. Here, we are using two inbuild modules of Python that is Datetime module and the Time Module to get the time in milliseconds.

Get time in milliseconds using the DateTime module

To get the time in milliseconds we used the DateTime module, Firstly we printed the date time with seconds. In another line, we printed the milliseconds.

Python3




import datetime
dt = datetime.datetime.now()
print(dt)
dt.microsecond / 1000


Output:

2022-09-20 00:28:51.400420
400.42

Get time in milliseconds using the Time module

The time.time() method of the Time module is used to get the time in seconds since the epoch. The handling of leap seconds is platform-dependent.

Python3




# Import class time from time module
from time import time
 
milliseconds = int(time() * 1000)
 
print("Time in milliseconds since epoch", milliseconds)


Output:

Time in milliseconds since epoch 1576071104408

Note: The epoch is the point where the time starts and is platform-dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC), and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS