To get a date string with milliseconds, use [:-3] to trim the last three digits of %f (microseconds):

>>> from datetime import datetime
>>> datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
'2022-09-24 10:18:32.926'

Or shorter:

>>> from datetime import datetime
>>> datetime.utcnow().strftime('%F %T.%f')[:-3]
'2022-09-24 10:18:32.926'

See the Python docs for more "%" format codes and the strftime(3) man page for the full list.

Answer from Jeremy Moritz on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
'seconds': Include hour, minute, and second in HH:MM:SS format. 'milliseconds': Include full time, but truncate fractional second part to milliseconds. HH:MM:SS.sss format. 'microseconds': Include full time in HH:MM:SS.ffffff format.
🌐
Python.org
discuss.python.org › ideas
Add millisecond formatting support to datetime.strftime - Ideas - Discussions on Python.org
November 28, 2025 - Hi, I’d like to propose adding native support for millisecond precision formatting in Python’s datetime.strftime() API. Currently, Python provides: %f in strftime() for microseconds (6 digits) datetime.isoformat(…
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python - GeeksforGeeks
April 17, 2025 - Here's how to do it: ... from datetime ... = datetime.strptime(s, format) print(dt_obj) ... Use %f when you want to include milliseconds or microseconds in your datetime string....
🌐
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 - We can directly pass the datetime object to the str() function to get the string in the standard date and time format. This method is faster than the above methods, but we could specify the string format. We can also simply remove the last three digits from the string to get the final result in milliseconds. Consider a scenario where you need to capture the current date and time, transforming it into a string with milliseconds for further processing or display. Python ...
🌐
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 - my_string1 = my_datetime.isoformat(sep = ' ', timespec = 'milliseconds') print(my_string1) # 2021-11-25 13:36:44.396 · As you can see, we have created a new data object called my_string1 that contains our example date with milliseconds formatted as a string.
Find elsewhere
🌐
GitHub
github.com › selectel › tempo › issues › 1
milliseconds in strptime and strftime · Issue #1 · selectel/tempo
June 25, 2012 - tempo:parse(iso8601, {datetime, }). ** exception error: no match of right hand side value {error,format_mism...
Author   2garryn
🌐
GitHub
github.com › moment › luxon › issues › 1156
Unable to parse 7 digit milliseconds · Issue #1156 · moment/luxon
March 8, 2022 - To Reproduce We have tried different combinations: DateTime.fromFormat('2022-01-25 00:06:30.1590000', 'yyyy-MM-dd HH:mm:ss.S') DateTime.fromFormat('2022-01-25 00:06:30.1590000', 'yyyy-MM-dd HH:mm:ss.SSSSSS') Actual vs Expected behavior Expected ...
Author   w-a-b
🌐
Esri Community
community.esri.com › t5 › python-questions › milliseconds-with-strftime › td-p › 73258
Solved: Milliseconds with strftime - Esri Community
December 10, 2021 - from datetime import datetime curr_time = datetime.now() formatted_time = curr_time.strftime('%H:%M:%S.%f') print(formatted_time) 18:41:59.891075‍‍‍‍‍‍‍‍‍‍‍‍‍‍ · How to get min, seconds and milliseconds from datetime.now() in Python
🌐
WordPress
mike632t.wordpress.com › 2021 › 05 › 02 › formatting-time-with-milliseconds-in-python
Formatting time with milliseconds in Python | Notes on Linux
May 2, 2021 - >>> _now = time.time() >>> print ("Time : %s.%s\n" % (time.strftime('%x %X',time.localtime(_now)), ... str('%.3f'%_now).split('.')[1])) # Rounds to nearest millisecond Time : 05/02/21 01:16:58.676 >>> The nice thing about this approach is that because the number of seconds is being formatted as a float the fractional part will be rounded correctly regardless of the number of decimal places used. >>> print ("Time : %s.%s\n" % (time.strftime('%x %X',time.localtime(_now)), ... str('%.1f'%_now).split('.')[1])) # Rounds to nearest tenth of a second Time : 05/02/21 01:16:58.7 >>>
🌐
GitHub
github.com › bitwalker › timex › issues › 322
strftime formatter does not pad microseconds when fractional part is equal to zero · Issue #322 · bitwalker/timex
June 28, 2017 - iex(1)> Timex.to_datetime({2017, 1, 1}) |> Timex.format("%f", :strftime) {:ok, "0"} Expected result: {:ok, "000000"} For example, in Python: >>> from datetime import datetime >>> datetime.strftime(datetime(2017, 1, 1), '%f') '000000' Reactions are currently unavailable ·
Author   narkq
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python
October 13, 2023 - The following example shows the ... is passed as the format parameter to this method, the formatting will default to "%a %b %d %H:%M:%S %Y" making it similar to the return value of the ctime() method....
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-parse-a-time-string-containing-milliseconds-in-python
How to Parse a Time String Containing Milliseconds in Python? - GeeksforGeeks
July 23, 2025 - This article will guide us through the process of parsing time strings containing milliseconds using Python's datetime module. Python's datetime module provides classes for manipulating dates and times. The most commonly used classes are datetime, date, time, and timedelta. These classes allow us to perform various operations, from simple date and time arithmetic to complex formatting and parsing of time strings.
🌐
sqlpey
sqlpey.com › python › formatting-datetime-to-string-with-milliseconds
Solved: Formatting Datetime to String with Milliseconds in Python
December 5, 2024 - If you are using Python 3.6 or a later version, the simplest way to format a datetime object to a string with milliseconds is to leverage the isoformat method.
🌐
Esri Community
community.esri.com › t5 › arcgis-pro-questions › time-format-with-milliseconds › td-p › 1291016
Solved: Time format with milliseconds - Esri Community
May 19, 2023 - Convert Time Field indeed seems to ignore milliseconds. Instead, you can use Calculate Field with this Python expression: datetime.datetime.strptime(!TextField1!, "%d-%m-%Y %H:%M:%S.%f")