Search code examples
pythonpython-2.7datetimeutc

How to print time in exact UTC format "2015-05-28T22:55:00:000Z" in python27


I need to see output in above-mentioned format. I tried:

  1. datetime.datetime.utcnow().isoformat()[:-4]

  2. datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S:%f')[:-4]

it does not include Z at the end.


Solution

  • Just add Z

    datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S:%f')[:-4] + 'Z'