Skip to content
Related Articles

Related Articles

Get current time in milliseconds using Python
  • Last Updated : 27 Dec, 2019

Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules, so there is no need to install it externally.

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

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).

Syntax: time.time()

Return type: This method returns a float value which represents the time in seconds since the epoch.



Example 1:




# Python program to get
# time in milliseconds
  
# Import class time from time module
from time import time
  
# time() function is multiplied with 
# 1000 to convert seconds into milliseconds.
milliseconds = int(time() * 1000)
  
print("Time in milliseconds since epoch", milliseconds)

Output:

Time in milliseconds since epoch 1576071104408

Example 2: Using lambda function




# Python program to get
# time in milliseconds
  
  
# Import class time from time module
from time import time
  
# Get time in milliseconds using
# lambda function
milliseconds = lambda: int(time() * 1000)
  
print("Time in milliseconds since epoch", milliseconds())

Output:

Time in milliseconds since epoch 1576071316487

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :