Python Language Get an ISO 8601 timestamp


Example

Without timezone, with microseconds

from datetime import datetime

datetime.now().isoformat()
# Out: '2016-07-31T23:08:20.886783'

With timezone, with microseconds

from datetime import datetime
from dateutil.tz import tzlocal

datetime.now(tzlocal()).isoformat()
# Out: '2016-07-31T23:09:43.535074-07:00'

With timezone, without microseconds

from datetime import datetime
from dateutil.tz import tzlocal

datetime.now(tzlocal()).replace(microsecond=0).isoformat()
# Out: '2016-07-31T23:10:30-07:00'

See ISO 8601 for more information about the ISO 8601 format.