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
'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. ... Excluded time components are truncated, not rounded.
Discussions

python - How to convert milliseconds to date and time format? - Stack Overflow
I have a value in milliseconds in a Python program. For example: 1557975599999 · And I would like to convert it to a string with days and hours, minutes, seconds. How can I do this? ... Is it correct to assume that this value is a "unix timestamp" - time since the epoch? Is it actually in microseconds, not in milliseconds? ... To convert unix timestamp to datetime... More on stackoverflow.com
🌐 stackoverflow.com
[IGN-6614]What is the best way to format a date (milliseconds) for a specified timezone with Ignition's python scripting
What is the best/simplest way to format a date (milliseconds) to a string equivalent conforming to a specified timezone using python scripting? The system.date.format() function does not appear to provide capability to specify the timezone you want the output to conform to. More on forum.inductiveautomation.com
🌐 forum.inductiveautomation.com
1
0
September 14, 2022
python - Datetime in ISO format with milliseconds to 000 - Stack Overflow
I'm voting to close this, as was pointed out this is a trivial formatting question. ... You want .replace(microsecond=0) to zero out the milli-(and micro-)seconds. Then just specify the right timespec for isoformat, 'milliseconds': nowzeroed = datetime.now(timezone.utc).replace(microsecond=0) ... More on stackoverflow.com
🌐 stackoverflow.com
Convert python datetime to timestamp in milliseconds - Stack Overflow
How do I convert a human-readable time such as 20.12.2016 09:38:42,76 to a Unix timestamp in milliseconds? ... You need to provide an appropriate format string to datetime.datetime.strptime. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 › python › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python - GeeksforGeeks
July 23, 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....
🌐
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 - Here's a quick test of the Converted ... Field with this Python expression: datetime.datetime.strptime(!TextField1!, "%d-%m-%Y %H:%M:%S.%f")...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-min-seconds-and-milliseconds-from-datetime-now-in-python
How to get min, seconds and milliseconds from datetime.now() in Python?
August 28, 2025 - from datetime import datetime now = datetime.now() minutes = now.minute seconds = now.second milliseconds = now.microsecond // 1000 time_str = f"{minutes:02d}:{seconds:02d}.{milliseconds:03d}" print("Time (MM:SS.ms):", time_str) # Alternative format time_str_alt = f"Minutes: {minutes}, Seconds: {seconds}, Milliseconds: {milliseconds}" print("Alternative format:", time_str_alt) ... microsecond vs millisecond: Python's datetime returns microseconds (1/1,000,000 second).
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_get_the_current_time_in_milliseconds_in_python
How do I get the current time in milliseconds in Python?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
Find elsewhere
🌐
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.
🌐
PYnative
pynative.com › home › python › python datetime › python datetime format using strftime()
Python DateTime Format using Strftime() – PYnative
May 6, 2022 - Now we can see more combinations with examples for a better understanding of the format codes that you can use to format dates in Python. The numerical format means to display the day, month, year, hours, minutes, seconds in numbers. like, 2021-07-07 12:19:47.864519 · from datetime import datetime # Get current Date x_date = datetime.now() print('Current Date:', x_date) # Represent Dates in numerical format print("dd-mm-yyyy HH:MM:SS:", x_date.strftime("%d-%m-%y %H:%M:%S")) print("dd-mm-yyyy:", x_date.strftime("%d-%m-%Y")) print("dd-mm-yy Format:", x_date.strftime("%d-%m-%y"))Code language: Python (python) Run
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to remove milliseconds from python datetime
5 Best Ways to Remove Milliseconds from Python datetime - Be on the Right Side of Change
February 28, 2024 - This effectively removes the milliseconds from the datetime. The strftime() method formats a datetime object as a string according to a specified format.
🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
The format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime(). If string cannot be parsed according to format, or if it has excess data after ...
🌐
Restack
restack.io › p › datetime-manipulation-techniques-knowledge-answer-datetime-format-python-milliseconds
Datetime Format Python Milliseconds | Restackio
May 3, 2025 - In this example, the // 1000 operation converts microseconds to milliseconds, and the :03 format specifier ensures that the output is always three digits, padding with zeros if necessary. By utilizing these techniques, you can effectively manage datetime formatting in Python, ensuring that your applications can handle time data with the precision required for modern programming tasks.
🌐
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 - 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.
🌐
YouTube
youtube.com › codenode
python datetime format string milliseconds - YouTube
Instantly Download or Run the code at https://codegive.com sure, here's a tutorial on using python's datetime module to format milliseconds in date and time...
Published   March 30, 2024
Views   6