0

I'm using Python 2.7.

I have a datetime object:

mytime = 2016-02-16 16:01:52.619388+00:00

I need to convert it to unicode in the following format:

unicode_time = u'2016-02-16T15:34:14.825878Z'

The way I accomplish this:

unicode_time = unicode(datetime.isoformat(mytime)).replace('+00:00', 'Z')

However, I don't like calling the .replace() method, it feels hacky to me. Is there a better way to do this? (that is supported with datetime module maybe)

Saša Kalaba
  • 4,241
  • 6
  • 29
  • 52

1 Answers1

0

You can use [:-6] to strip the +00:00 string and then add Z.

import datetime as dt

>>> unicode(dt.datetime.isoformat(mytime)[:-6] + 'Z')
u'2016-02-16T16:01:52.619388Z'
Alexander
  • 105,104
  • 32
  • 201
  • 196