How to convert a datetime string to millisecond UNIX time stamp?



A millisecond UNIX timestamp is a number that shows how many milliseconds have elapsed since the beginning of the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC) up to the current moment or specified date and time. Instead of counting time in days, hours, or minutes, it counts in milliseconds (1 second = 1000 milliseconds).

In Python, the common way to convert a Datetime object to a milliseconds timestamp involves using the strptime() function (to parse a string into a Datetime object), then converting this datetime object into a UNIX timestamp by using the timestamp() method, and finally multiplying the result value by 1000 to get in milliseconds.

The following are the other methods to convert a datetime string to a millisecond UNIX time stamp:

Using strptime() Function and timestamp() Method

The strptime() function from the datetime module parses a string representing a time, either in UTC or local time, according to a format. This method accepts two arguments (the time to be parsed as a string and the specified format).

After parsing a string into a datetime object, the timestamp() method can be called to get the UNIX timestamp in seconds. To convert it to milliseconds, simply multiply the result by 1000.

Example

Let's say we have a string "2025-06-18 14:30:00" and we want to convert it to a millisecond UNIX timestamp. In the following program, we will parse this string using strptime(), then convert it to a timestamp using timestamp(), and finally multiply by 1000.

from datetime import datetime

dt_string = "2025-06-18 14:30:00"
dt_object = datetime.strptime(dt_string, "%Y-%m-%d %H:%M:%S")
ms_timestamp = int(dt_object.timestamp() * 1000)
print(ms_timestamp)

Following is the output of the above code:

1755556200000

Using pandas.to_datetime() Method

The to_datetime() function provided by the pandas library can also be used to convert a datetime string into a Timestamp object. By using the value attribute, we get the time in nanoseconds, but we want it in milliseconds, so we have to divide this value by one million (1,000,000).

Example

The following program demonstrates how to convert a datetime string to a millisecond UNIX time stamp by using pandas.to_datetime() method.

from datetime import datetime
import pandas as pd

dt_string = "2025-06-18 14:30:00"
timestamp_ms = pd.to_datetime(dt_string).value // 10**6

print(timestamp_ms)

Following is the output of the above code:

1755556200000

Using time.mktime() Method

The mktime() method from the time module converts a time tuple (also called struct_time) into seconds since the Unix Epoch (January 1, 1970). First, we use the strptime() function to convert the datetime string into a datetime object.

Then, we convert this object into a time tuple using the timetuple() method (returns time.struct_time object). Finally, we pass this time tuple to mktime() to get the timestamp in seconds and multiply the result by 1000 to get the timestamp in milliseconds.

Example

The following example demonstrates an alternative way to convert a datetime string to millisecond UNIX time stamp by using time module instead of the datetime module.

import time
from datetime import datetime

dt_string = "2025-06-18 14:30:00"
time_tuple = datetime.strptime(dt_string, "%Y-%m-%d %H:%M:%S").timetuple()
timestamp_ms = int(time.mktime(time_tuple) * 1000)

print(timestamp_ms)

Following is the output of the above code:

1755556200000
Updated on: 2025-08-28T11:52:42+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements