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 string 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 milliseconds.

Using strptime() and timestamp() Methods

The strptime() function from the datetime module parses a string representing a time according to a format. This method accepts two arguments: the time string to be parsed and the format specification.

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, multiply the result by 1000.

Example

Let's convert the string "2025-06-18 14:30:00" to a millisecond UNIX timestamp ?

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(f"Millisecond timestamp: {ms_timestamp}")
Millisecond timestamp: 1755556200000

Using pandas.to_datetime() Method

The to_datetime() function from the pandas library can convert a datetime string into a Timestamp object. Using the value attribute, we get the time in nanoseconds, so we divide by one million (1,000,000) to get milliseconds.

Example

The following program demonstrates converting a datetime string using pandas ?

import pandas as pd

dt_string = "2025-06-18 14:30:00"
timestamp_ms = pd.to_datetime(dt_string).value // 10**6
print(f"Millisecond timestamp: {timestamp_ms}")
Millisecond timestamp: 1755556200000

Using time.mktime() Method

The mktime() method from the time module converts a time tuple (struct_time) into seconds since the Unix Epoch. First, parse the datetime string with strptime(), convert to a time tuple using timetuple(), then pass to mktime() and multiply by 1000.

Example

Here's how to use the time module for conversion ?

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(f"Millisecond timestamp: {timestamp_ms}")
Millisecond timestamp: 1755556200000

Comparison of Methods

Method Module Required Best For
strptime() + timestamp() datetime (built-in) Standard Python approach
pandas.to_datetime() pandas Data analysis workflows
time.mktime() time (built-in) Legacy time handling

Conclusion

Use datetime.strptime() with timestamp() for standard conversion. The pandas approach is ideal when working with data analysis, while time.mktime() provides an alternative using the time module.

Updated on: 2026-03-24T19:26:21+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements