datetime.datetime.utcnow().strftime('%Y-%m%d %H:%M:%S.000%f')

Seems to work for me.

Physically you should do this to get the correct representation of the time:

datetime.datetime.utcnow().strftime('%Y-%m%d %H:%M:%S.%f000')
Answer from ljk321 on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
'hours': Include the hour in the two-digit HH format. 'minutes': Include hour and minute in HH:MM format. '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. ... Excluded time components are truncated, not rounded. ValueError will be raised on an invalid timespec argument: >>> import datetime as dt >>> dt.datetime.now().isoformat(timespec='minutes') '2002-12-25T00:00' >>> my_datetime = dt.datetime(2015, 1, 1, 12, 30, 59, 0) >>> my_datetime.isoformat(timespec='microseconds') '2015-01-01T12:30:59.000000'
Discussions

milliseconds in strptime and strftime
tempo:parse(iso8601, {datetime, }). ** exception error: no match of ... More on github.com
🌐 github.com
8
June 25, 2012
python - Format a datetime into a string with milliseconds - Stack Overflow
How can I format a datetime object as a string with milliseconds? More on stackoverflow.com
🌐 stackoverflow.com
Add millisecond formatting support to datetime.strftime - Ideas - Discussions on Python.org
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(timespec="milliseconds") for ISO output But there is no way to format milliseconds ... More on discuss.python.org
🌐 discuss.python.org
6
November 28, 2025
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? More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › selectel › tempo › issues › 1
milliseconds in strptime and strftime · Issue #1 · selectel/tempo
June 25, 2012 - It is impossible to parse string with milliseconds. For example: (tempo_dev@garry-desktop)22> tempo:parse(iso8601, {datetime, <<"2012-06-16T08:25:34.123+01:00">>}).
Author   2garryn
🌐
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....
🌐
Zditect
zditect.com › blog › 53308276.html
python strptime milliseconds 3 digits
We cannot provide a description for this page right now
🌐
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 ... (6 digits) datetime.isoformat(timespec="milliseconds") for ISO output But there is no way to format milliseconds ......
Find elsewhere
🌐
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.
🌐
Esri Community
community.esri.com › t5 › python-questions › milliseconds-with-strftime › td-p › 73258
Solved: Milliseconds with strftime - Esri Community
December 10, 2021 - (I can appended i at the end of new Time as a phony millisecond...) That should just about do it.... Solved! Go to Solution. ... from datetime import datetime curr_time = datetime.now() formatted_time = curr_time.strftime('%H:%M:%S.%f') print(formatted_time) 18:41:59.891075‍‍‍‍‍‍‍‍‍‍‍‍‍‍
🌐
py4u
py4u.org › blog › python-time-milli-seconds-calculation
How to Calculate Milliseconds in Python Using datetime Module: A Practical Guide
To include milliseconds in a formatted string, manually extract the microseconds, convert to milliseconds, and append them to the string. ... from datetime import datetime # Create a datetime object with microseconds dt = datetime(2024, 5, 20, ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python
October 13, 2023 - The Python time strptime() method parses a string representing a time, either in UTC or local time, according to a format. The method accepts two arguments: one being the time to be parsed as a string and the other argument is the format specified.
🌐
GeeksforGeeks
geeksforgeeks.org › python › parsing-datetime-strings-containing-nanoseconds-in-python
Parsing DateTime strings containing microseconds in Python - GeeksforGeeks
July 23, 2025 - "%f" directive is used to parse the microseconds. The same thing is cross-verified by converting the "%f" directive to the string value of its representation for the datetime object using the strftime() 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 - We can use the strptime method to parse a string into a datetime object, specifying the exact format of our time string. ... Milliseconds are a thousandth of a second, and since datetime handles microseconds, we can use %f to capture them.
🌐
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 - If you are prepared to store the time in a variable and do a little string manipulation, then you can display the date and time including the milliseconds without using the datetime module. >>> _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…
🌐
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