How to convert Python DateTime string into integer milliseconds?

Python provides the time and datetime modules to convert a DateTime string into integer milliseconds. Key functions include time.time(), which gives the current time in seconds, and datetime.timestamp(), which converts datetime objects directly into seconds since the epoch. By multiplying these values by 1000, we can get the time in milliseconds.

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.

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.

Example

In the following program, we use the time.gmtime(0) to get the epoch on a given platform and time.time() method to get the current time in seconds. Then we 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 ?

The epoch is: Thu Jan  1 00:00:00 1970
Milliseconds since epoch: 1662373873162

Using datetime.utcnow() and Subtracting Epoch

Initially, we retrieve the current date by using the datetime.utcnow() method. Then we get the number of days since the epoch by subtracting the date (datetime(1970, 1, 1)) from the current date. For this date, we apply the .total_seconds() method to get 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 retrieve the current UTC using the utcnow() method, subtract the epoch date datetime(1970, 1, 1) to get the time difference, then extract total seconds using the total_seconds() method (calculates the total number of seconds represented by a timedelta object), and convert it to milliseconds ?

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)

The output of the above code is ?

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

Using timestamp() Function on a Specific Date

The timestamp() function from the datetime module is used to convert any datetime object to seconds since the epoch (floating-point number). This function helps convert a specific date rather than the current time.

Example

In the following program, we create a datetime object for January 1, 2018, using the datetime() method, then convert it into seconds since the epoch using the timestamp() method, and finally convert it into milliseconds (multiply by 1000) using the round() function ?

import time
from datetime import datetime

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

The output of the above code is ?

1514745000000

Converting DateTime String to Milliseconds

To convert a datetime string to milliseconds, first parse the string using strptime(), then use timestamp() to get seconds since epoch ?

from datetime import datetime

date_string = "2023-05-15 14:30:25"
dt = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
milliseconds = int(dt.timestamp() * 1000)

print(f"DateTime string: {date_string}")
print(f"Milliseconds: {milliseconds}")

The output of the above code is ?

DateTime string: 2023-05-15 14:30:25
Milliseconds: 1684155025000

Comparison

Method Use Case Advantage
time.time() Current time only Simple and fast
datetime.utcnow() Current UTC time More control over operations
timestamp() Specific datetime objects Works with any datetime
strptime() + timestamp() Converting datetime strings Handles string parsing

Conclusion

Use time.time() for current time in milliseconds, timestamp() for specific datetime objects, and combine strptime() with timestamp() to convert datetime strings to milliseconds.

Updated on: 2026-03-24T19:25:18+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements