There is the no directive mentioned in time.strftime(...) that will return you the milliseconds. Not sure from where you got the reference to use %f. In fact time.gmtime(...) holds the precision only upto seconds.

As a hack, in order to achieve this, you may explicitly format your string by preserving your milli second value as:

>>> import time

>>> time_in_ms = 1515694048121
>>> time.strftime('%Y-%m-%d %H:%M:%S:{}'.format(time_in_ms%1000), time.gmtime(time_in_ms/1000.0))
'2018-01-11 18:07:28:121'

Here's the list of valid directives:

+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| Directive |                                                                                                   Meaning                                                                                                   | Notes |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| %a        | Locale’s abbreviated weekday name.                                                                                                                                                                          |       |
| %A        | Locale’s full weekday name.                                                                                                                                                                                 |       |
| %b        | Locale’s abbreviated month name.                                                                                                                                                                            |       |
| %B        | Locale’s full month name.                                                                                                                                                                                   |       |
| %c        | Locale’s appropriate date and time representation.                                                                                                                                                          |       |
| %d        | Day of the month as a decimal number [01,31].                                                                                                                                                               |       |
| %H        | Hour (24-hour clock) as a decimal number [00,23].                                                                                                                                                           |       |
| %I        | Hour (12-hour clock) as a decimal number [01,12].                                                                                                                                                           |       |
| %j        | Day of the year as a decimal number [001,366].                                                                                                                                                              |       |
| %m        | Month as a decimal number [01,12].                                                                                                                                                                          |       |
| %M        | Minute as a decimal number [00,59].                                                                                                                                                                         |       |
| %p        | Locale’s equivalent of either AM or PM.                                                                                                                                                                     | (1)   |
| %S        | Second as a decimal number [00,61].                                                                                                                                                                         | (2)   |
| %U        | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.                                | (3)   |
| %w        | Weekday as a decimal number [0(Sunday),6].                                                                                                                                                                  |       |
| %W        | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.                                | (3)   |
| %x        | Locale’s appropriate date representation.                                                                                                                                                                   |       |
| %X        | Locale’s appropriate time representation.                                                                                                                                                                   |       |
| %y        | Year without century as a decimal number [00,99].                                                                                                                                                           |       |
| %Y        | Year with century as a decimal number.                                                                                                                                                                      |       |
| %z        | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |       |
| %Z        | Time zone name (no characters if no time zone exists).                                                                                                                                                      |       |
| %%        |                                                                                                                                                                                                             |       |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
Answer from Moinuddin Quadri on Stack Overflow
🌐
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
🌐
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 - To include milliseconds while parsing time, the %f format code is used. For example, let's consider we have a timestamp string like this: "15/06/2021 13:30:15.120", the string returns a specific date and time including milliseconds (.120).
Top answer
1 of 3
10

There is the no directive mentioned in time.strftime(...) that will return you the milliseconds. Not sure from where you got the reference to use %f. In fact time.gmtime(...) holds the precision only upto seconds.

As a hack, in order to achieve this, you may explicitly format your string by preserving your milli second value as:

>>> import time

>>> time_in_ms = 1515694048121
>>> time.strftime('%Y-%m-%d %H:%M:%S:{}'.format(time_in_ms%1000), time.gmtime(time_in_ms/1000.0))
'2018-01-11 18:07:28:121'

Here's the list of valid directives:

+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| Directive |                                                                                                   Meaning                                                                                                   | Notes |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| %a        | Locale’s abbreviated weekday name.                                                                                                                                                                          |       |
| %A        | Locale’s full weekday name.                                                                                                                                                                                 |       |
| %b        | Locale’s abbreviated month name.                                                                                                                                                                            |       |
| %B        | Locale’s full month name.                                                                                                                                                                                   |       |
| %c        | Locale’s appropriate date and time representation.                                                                                                                                                          |       |
| %d        | Day of the month as a decimal number [01,31].                                                                                                                                                               |       |
| %H        | Hour (24-hour clock) as a decimal number [00,23].                                                                                                                                                           |       |
| %I        | Hour (12-hour clock) as a decimal number [01,12].                                                                                                                                                           |       |
| %j        | Day of the year as a decimal number [001,366].                                                                                                                                                              |       |
| %m        | Month as a decimal number [01,12].                                                                                                                                                                          |       |
| %M        | Minute as a decimal number [00,59].                                                                                                                                                                         |       |
| %p        | Locale’s equivalent of either AM or PM.                                                                                                                                                                     | (1)   |
| %S        | Second as a decimal number [00,61].                                                                                                                                                                         | (2)   |
| %U        | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.                                | (3)   |
| %w        | Weekday as a decimal number [0(Sunday),6].                                                                                                                                                                  |       |
| %W        | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.                                | (3)   |
| %x        | Locale’s appropriate date representation.                                                                                                                                                                   |       |
| %X        | Locale’s appropriate time representation.                                                                                                                                                                   |       |
| %y        | Year without century as a decimal number [00,99].                                                                                                                                                           |       |
| %Y        | Year with century as a decimal number.                                                                                                                                                                      |       |
| %z        | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |       |
| %Z        | Time zone name (no characters if no time zone exists).                                                                                                                                                      |       |
| %%        |                                                                                                                                                                                                             |       |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
2 of 3
5

You can easily use the datetime library to format microseconds using %f.

│    >>> from datetime import datetime

     >>> datetime.utcnow().strftime("%Y%m%d-%H:%M:%S")
     '20211018-16:02:38'
    
     >>> datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")
     '20211018-16:02:38.986417'
🌐
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 - 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)), ...
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
>>> import datetime as dt >>> delta = dt.timedelta( ... days=50, ... seconds=27, ... microseconds=10, ... milliseconds=29000, ... minutes=5, ... hours=8, ... weeks=2 ...
🌐
GitHub
github.com › pola-rs › polars › issues › 4575
Milliseconds are lost when using `.str.strftime` · Issue #4575 · pola-rs/polars
August 26, 2022 - Polars version checks I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of polars. Issue Description Polars seems to lose the information about milliseconds after convert...
Author   danielgafni
Find elsewhere
🌐
Python
bugs.python.org › issue1982
Issue 1982: Feature: extend strftime to accept milliseconds - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/46274
🌐
EyeHunts
tutorial.eyehunts.com › home › python strftime function | milliseconds | examples
Python strftime Function | milliseconds | Examples - EyeHunts
May 18, 2021 - import datetime # YYYY-MM-DD HH:MM:SS.MS timezone xt = datetime.datetime(2018, 10, 10, 3, 38, 1, 844628, tzinfo=datetime.timezone(datetime.timedelta(0, 3600))) print(xt) print(xt.strftime("%a")) ... It can be an interview question for a company, ...
🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
0 is a legal argument for any position in the time tuple; if it is normally illegal the value is forced to a correct one. The following directives can be embedded in the format string. They are shown without the optional field width and precision specification, and are replaced by the indicated characters in the strftime() result:
🌐
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 - The resulting string includes the date and time in the specified format, with microseconds appended. To convert this to milliseconds, we slice the string using [:-3], excluding the last three characters.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-min-seconds-and-milliseconds-from-datetime-now-in-python
How to get min, seconds and milliseconds from datetime.now() in Python?
2 weeks ago - from datetime import datetime now ... Python's datetime returns microseconds (1/1,000,000 second). To get milliseconds, divide by 1000. strftime() format codes: Use %M for minutes, %S for seconds, and %f for microsec...
🌐
Python
python-list.python.narkive.com › Ai2VOU1o › outputting-time-in-microseconds-or-milliseconds
outputting time in microseconds or milliseconds
It would really be great if python had a way to just tack the microseconds (actually i think milliseconds would be better) on to the second (in the 2nd column) as a fraction. If this is not possible I need the extra write statement for just the microseconds (millisecond if possible). now = time() #make a single call to get the time in seconds (float) localnow = localtime(now) print(strftime("%Y-%m-%d\t%H:%M:%S\t", llocalnow)) print(now-int(now)) #obviously you should format this to get the desired layout
🌐
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.
🌐
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 ... a string. In this example, we’ll use the strftime() function to convert a datetime object to a character string with milliseconds and microseconds....
🌐
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.