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 - Sometimes, we may want to create a custom time string using the input values obtained from datetime.now() method. We can combine .minute, .second, and the converted .microsecond for this. The following code gets the current time, extracts minutes, seconds, and converts microseconds to milliseconds, then formats and prints it as a string in the "Minutes: Seconds: Milliseconds" format.
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
๐ŸŒ
Domo
community-forums.domo.com โ€บ home โ€บ community forums โ€บ connectors
Remove milliseconds from date - Domo Community Forum
May 17, 2024 - I am building a connector which requires the date in the API call, however it needs to be format YYYY-MM-DDTHH:MM:SSZ. today = new Date(); provides the date however it also includes milliseconds. How can I get the date without milliseconds or remove them after getting the date?