[Python] How to get ISO8601 string for datetime with milliseconds instead of microseconds in...

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Given the following datetime:

    d = datetime.datetime(2018, 10, 9, 8, 19, 16, 999578, tzinfo=dateutil.tz.tzoffset(None, 7200))


    d.isoformat() results in the string:

    '2018-10-09T08:19:16.999578+02:00'


    How can I get a string with milliseconds instead of microseconds:

    '2018-10-09T08:19:16.999+02:00'


    strftime() will not work here: %z returns 0200 istead of 02:00 and has only %f to get microseconds, there is no placeholder for milliseconds.

    Continue reading...

Compartilhe esta Página