
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get current time in milliseconds in Python?
We can retrieve the current time in milliseconds using different Python built-in modules like time, datetime, and calendar. These modules provide functions to work with time and dates.
Each method gives us the time in a different format, but with a little calculation, we can convert it to milliseconds. The following are several methods used to achieve this.
Using time.time() Method
The time module in Python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch. It returns a floating-point number expressed in seconds. And then, this value is multiplied by 1000 and rounded off with the round() function.
Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch. We use time.gmtime(0) to get the epoch on a given platform.
Example
In the following program, we use the time.time() method to get the current time in seconds. We then multiply by 1000, and we approximate the value by using the round() function.
import time obj = time.gmtime(0) epoch = time.asctime(obj) print("The epoch is:",epoch) curr_time = round(time.time()*1000) print("Milliseconds since epoch:",curr_time)
The output of the above code is as follows:
The epoch is: Thu Jan 1 00:00:00 1970 Milliseconds since epoch: 1662372570512
Using the datetime Module
Here we use various functions that are provided by the datetime module to find the current time in milliseconds.
Initially, we retrieve the current date by using the datetime.utc() method. Then we get the number of days since the epoch by subtracting the date 01-01-1670 (datetime(1970, 1, 1)) from the current date. For this date, we apply the .total_seconds() returns the total number of seconds since the epoch. Finally, we round off the value to milliseconds by applying the round() function.
Example
In the following program, we get the current time in milliseconds by using different functions that are provided by the python datetime module.
from datetime import datetime print("Current date:",datetime.utcnow()) date= datetime.utcnow() - datetime(1970, 1, 1) print("Number of days since epoch:",date) seconds =(date.total_seconds()) milliseconds = round(seconds*1000) print("Milliseconds since epoch:",milliseconds)
The output of the above example code is as follows:
Current date: 2022-09-05 10:10:17.745855 Number of days since epoch: 19240 days, 10:10:17.745867 Milliseconds since epoch: 1662372617746
Using calendar.timegm() with datetime
We can also use the calendar module with a datetime combination. First, we retrieve the current UTC time, convert it to a tuple, and then use calendar.timegm() function to get the time in seconds since the epoch. Multiply by 1000 to get time in milliseconds.
Example
This program converts the current UTC datetime to milliseconds since the Unix epoch using the calendar and datetime modules. The now.timetuple() function creates a time.struct_time object from the 'now' datetime, and calendar.timegm() converts this to a UTC timestamp in seconds. Finally, the round() function rounds the calculated milliseconds to the nearest whole millisecond.
import calendar from datetime import datetime now = datetime.utcnow() epoch_seconds = calendar.timegm(now.timetuple()) milliseconds = round(epoch_seconds * 1000 + now.microsecond / 1000) print("Milliseconds since epoch:", milliseconds)
Following is the output of the above code:
Milliseconds since epoch: 1662372712984