# How to get current time in milliseconds in Python?

In this article, we will discuss the various way to retrieve the current time in milliseconds in python.

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

NOTE : 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.

### Syntax

The syntax of time() method is as follows −

time.time()


Returns a float value that represents the seconds since the epoch.

### Example

In the following example code, we use the time.time() method to get the current time in seconds. We then multiple 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)


### Output

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 example code, 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)


### Output

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