How can I get the current time in ISO formatted string with the 'Z' as time designator instead of '+00:00'"?
Question: On Python 3.x `datetime.utcnow().isoformat()` gives no timezone designator and
`datetime.now(timezone.utc).isoformat` gives the `+00:00`. Is there any way to
force to use the `Z` (zulu timezone)?


Answer: The ~~naive~~ straighforward way is

    
    
    from datetime import datetime, timezone
    datetime.utcnow().isoformat()+'Z' # '2015-06-09T07:17:55.719302Z'
    

`datetime.utcnow()` returns a naive `datetime` on UTC , `isoformat` won't add
any timezone because there is none in the naive datetime, and then we add
manually the `'Z'` because we know that it's actually in the UTC / Zulu
timezone

or if you happen to have a timezone aware `datetime` on UTC already you can
just drop the timezone (make a copy of the `datetime` first) and the do the
`.isoformat()+'Z'`.

    
    
    from datetime import datetime, timezone
    datetime.now(timezone.utc).replace(tzinfo=None).isoformat()+'Z'
    # '2015-06-09T07:17:55.719302Z'
    

If the timezone aware `datetime` is not on UTC then you need to bring it to
the UTC timezone first.

    
    
    from datetime import datetime,timezone
    from dateutil.tz import tzutc
    datetime.now(tzoffset("BRST", -10800)).astimezone(tzutc()).replace(tzinfo=None).isoformat()+'Z'
    # '2015-06-09T08:14:02.861058Z'
    

