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....
🌐
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.
🌐
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 provides a built-in solution using the str() method.
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 >>>
🌐
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....
🌐
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
🌐
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.
🌐
APIdock
apidock.com › ruby › v1_9_3_392 › DateTime › strftime
strftime (DateTime) - APIdock
+0900) %:z - hour and minute offset from UTC with a colon (e.g. +09:00) %::z - hour, minute and second offset from UTC (e.g. +09:00:00) %:::z - hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30) %Z - Time zone abbreviation name Weekday: %A - The full weekday name (``Sunday'') %^A uppercased (``SUNDAY'') %a - The abbreviated name (``Sun'') %^a uppercased (``SUN'') %u - Day of the week (Monday is 1, 1..7) %w - Day of the week (Sunday is 0, 0..6) ISO 8601 week-based year and week number: The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year. %G - The week-based year %g - The last 2 digits of the week-based year (00..99) %V - Week number of the week-based year (01..53) Week number: The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W).
🌐
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.