In Python 3 this can be done in 2 steps:

  1. Convert timestring to datetime object
  2. Multiply the timestamp of the datetime object by 1000 to convert it to milliseconds.

For example like this:

from datetime import datetime

dt_obj = datetime.strptime('20.12.2016 09:38:42,76',
                           '%d.%m.%Y %H:%M:%S,%f')
millisec = dt_obj.timestamp() * 1000

print(millisec)

Output:

1482223122760.0

strptime accepts your timestring and a format string as input. The timestring (first argument) specifies what you actually want to convert to a datetime object. The format string (second argument) specifies the actual format of the string that you have passed.

Here is the explanation of the format specifiers from the official documentation:

  • %d - Day of the month as a zero-padded decimal number.
  • %m - Month as a zero-padded decimal number.
  • %Y - Year with century as a decimal number
  • %H - Hour (24-hour clock) as a zero-padded decimal number.
  • %M - Minute as a zero-padded decimal number.
  • %S - Second as a zero-padded decimal number.
  • %f - Microsecond as a decimal number, zero-padded to 6 digits.
Answer from Milo on Stack Overflow
Top answer
1 of 3
23

There is no slot for the microseconds component in a time tuple:

>>> import time
>>> import datetime
>>> myDate = "2014-08-01 04:41:52,117"
>>> datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timetuple()
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=1, tm_hour=4, tm_min=41, tm_sec=52, tm_wday=4, tm_yday=213, tm_isdst=-1)

You'll have to add those manually:

>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> time.mktime(dt.timetuple()) + (dt.microsecond / 1000000.0)
1406864512.117

The other method you could follow is to produce a timedelta() object relative to the epoch, then get the timestamp with the timedelta.total_seconds() method:

epoch = datetime.datetime.fromtimestamp(0)
(dt - epoch).total_seconds()

The use of a local time epoch is quite deliberate since you have a naive (not timezone-aware) datetime value. This method can be inaccurate based on the history of your local timezone however, see J.F. Sebastian's comment. You'd have to convert the naive datetime value to a timezone-aware datetime value first using your local timezone before subtracting a timezone-aware epoch.

As such, it is easier to stick to the timetuple() + microseconds approach.

Demo:

>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> epoch = datetime.datetime.fromtimestamp(0)
>>> (dt - epoch).total_seconds()
1406864512.117
2 of 3
6

In Python 3.4 and later you can use

timestamp = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timestamp()

This doesn't require importing the time module. It also uses less steps so it should be faster. For older versions of python the other provided answers are probably your best option.

However, the resulting timestamp will interpret myDate in local time, rather than UTC, which may cause issues if myDate was given in UTC

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python - GeeksforGeeks
July 23, 2025 - The strptime() method from the datetime module in Python helps convert string representations into datetime objects. To include milliseconds while parsing time, the %f format code is used. For example, let's consider we have a timestamp string ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-python-datetime-string-into-integer-milliseconds
How to convert Python DateTime string into integer milliseconds?
August 28, 2025 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-parse-a-time-string-containing-milliseconds-in-python
How to Parse a Time String Containing Milliseconds in Python? - GeeksforGeeks
September 28, 2024 - To include milliseconds while parsing time, the %f format code is used. For example, let's consider we have a timestamp string like this: "15/06/2021 13:30:15.120", the string returns
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.html
pandas.Timestamp — pandas 3.0.1 documentation
class pandas.Timestamp(ts_input=<object object>, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, nanosecond=None, tz=<object object>, unit=None, fold=None)[source]# Pandas replacement for python datetime.datetime object.
Find elsewhere
🌐
GitHub
gist.github.com › sammchardy › fcbb2b836d1f694f39bddd569d1c16fe
Convert date to millisecond timestamp · GitHub
August 15, 2018 - Convert date to millisecond timestamp · Raw · convert-date-to-milliseconds.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-a-datetime-string-to-millisecond-UNIX-time-stamp
How to convert a datetime string to millisecond UNIX time stamp?
February 19, 2020 - 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 ...
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › convert datetime into string with milliseconds in python (3 examples)
Convert datetime into String with Milliseconds in Python (3 Examples)
January 26, 2023 - Do you need more explanations on how to change a date and time into a string with milliseconds in Python? Then have a look at the following video of the PyLenin YouTube channel. In the video, the speaker explains how to use strptime and strftime for converting strings to datetime objects and vice-versa.
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › convert milliseconds into datetime in python (example)
Convert Milliseconds into datetime (Python Example) | Create Date & Time
March 22, 2023 - Convert datetime Object to Date & Vice Versa in Python · Convert datetime into String with Milliseconds in Python · Python Programming Language · In summary: In this post, I have explained how to turn milliseconds into a datetime object in the Python programming language.
🌐
Delft Stack
delftstack.com › home › howto › python › python datetime milliseconds
How to Convert DateTime to String With Milliseconds in Python | Delft Stack
February 2, 2024 - The strftime() method returns a string based on a specific format specified as a string in the argument. In this section, we’ll delve into the concept of converting a datetime object to a string representation with milliseconds using Python’s ...
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.
🌐
Kontext
kontext.tech › home › blogs › code snippets & tips › convert timestamp to milliseconds since epoch in python
Convert Timestamp to Milliseconds since Epoch in Python - Kontext Labs
August 22, 2022 - This code snippets provides different approaches to convert a Python timestamp object to an integer that denotes the total milliseconds since Epoch (midnight 1st Jan 1970 UTC, UNIX time). ... *Your result can be different depends on the time when you run the code. The result is the same no matter if we use utcnow() or now() as they both represent the same time. However, if your input timestamp format is string...
🌐
Timestamp Converter
timestamp.online
Timestamp To Date Converter
Convert timestamp to date or date to timestamp easily. Learn how to convert timestamp to date in Python, PHP, JavaScript, Bash, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-date-string-to-timestamp-in-python
Convert Date String to Timestamp in Python - GeeksforGeeks
October 29, 2025 - datetime.timestamp() converts a datetime object to a Unix timestamp in seconds and returns a float. It assumes local time if no timezone is set. ... from datetime import datetime s = "20/01/2020" dt = datetime.strptime(s, "%d/%m/%Y") res = dt.timestamp() print(res) ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python
October 13, 2023 - If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised. Following is the syntax for the Python time strptime() method