I have datetime with UTC timezone and I want to convert it into timestamp string of format %Y-%M-%DT%h:%m:%.3sZ. So it should have military time zone at the end.

Only way I could figure out to do this was this:

>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace('+00:00', 'Z')
'2024-03-18T12:20:53.190Z'

But is there better way to do this?

0

There are 0 best solutions below