Search code examples
pythondjangopytzrfc3339

DateTime in ISO 8601 + TimeZone and without microseconds


I need date-time string in ISO 8601 without any microseconds.

Like:

2015-01-05T11:26:00-03:00

I use:

from pytz import timezone
from datetime import datetime
timezone(settings.TIME_ZONE).localize(datetime.now()).isoformat()

But it returns:

'2015-01-28T17:49:39.711725-03:00'

How to fix that?


Solution

  • Set microsecond as 0:

    t = timezone(settings.TIME_ZONE).localize(datetime.now()).replace(microsecond=0)
    t.isoformat()
    

    datetime.datetime.replace(...) will return a new datetime object with specified attribute modified.