How to convert Python DateTime string into integer milliseconds?


In this article, we will discuss the various way to convert the python datetime string to 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 which returns a floating-point number expressed in seconds. 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 with 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: 1662373873162

Using the datetime module

Here we use various functions that are provided by the datetime module to find the current time and convert this string to an integer millisecond.

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 1

In the following example code, we get the current time in string and convert to an integer millisecond.

from datetime import datetime
print("Current date in string format:",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 in string format: 2022-09-05 10:31:52.853660
Number of days since epoch: 19240 days, 10:31:52.853671
Milliseconds since epoch: 1662373912854

Example 2

The timestamp() function is used to convert a datetime object to milliseconds −

import time
from datetime import datetime
dt = datetime(2018, 1, 1)
milliseconds = int(round(dt.timestamp() * 1000))
print(milliseconds)

Output

This will give the output

1514745000000

Updated on: 04-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements