Hi,
I’d like to propose adding native support for millisecond precision formatting in Python’s datetime.strftime() API.
Currently, Python provides:
-
%finstrftime()for microseconds (6 digits) -
datetime.isoformat(timespec="milliseconds")for ISO output
But there is no way to format milliseconds declaratively using strftime().
Rationale
I’ve been doing backend web development with Python and Django for about 10 years, mostly in REST APIs.
In virtually every Django codebase I’ve worked on, I’ve seen patterns like:
dt.isoformat(timespec="milliseconds").replace("+00:00", "Z")
or:
dt.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
These are everywhere in web projects.
The reason is simple: in JavaScript and JSON-based APIs, the de-facto standard datetime format is:
YYYY-MM-DDTHH:mm:ss.sssZ
-
millisecond precision
-
ISO-8601
-
Zfor UTC
Python cannot generate this format declaratively using strftime().
You either slice strings or post-process output, which defeats the point of formatting directives.
This becomes a real problem in frameworks like Django REST Framework, where datetime formatting is configured using format strings, not functions. You cannot call Python logic there without subclassing and overriding serializers — most projects don’t, and end up with fragile hacks instead.
Precedent
Other web-centric languages offer native millisecond formatters:
-
Ruby:
%L(milliseconds) -
PHP:
v(milliseconds)
This indicates that “milliseconds in formatting” is not an exotic requirement — it’s routine web plumbing.
Scope
This proposal does not aim to solve UTC rendering (Z vs +00:00) — that is a separate issue.
The goal here is strictly:
Provide a first-class way to express millisecond precision in strftime()
without string slicing or post-processing.
If this sounds acceptable conceptually, I’d be happy to help implement it (if it’s a python-only thing, I can do it with just little guidance).
Thanks.