I'm trying to serialize datetime in an API, but I don't want milliseconds. What I want is here: https://en.wikipedia.org/wiki/ISO_8601 - "2015-09-14T17:51:31+00:00"
tz = pytz.timezone('Asia/Taipei') dt = datetime.datetime.now() loc_dt = tz.localize(dt) Try A:
loc_dt.isoformat() >> '2015-09-17T10:46:15.767000+08:00' Try B:
loc_dt.strftime("%Y-%m-%dT%H:%M:%S%z") >> '2015-09-17T10:46:15+0800' The latter one is almost perfect except it's missing the colon in the timezone part. How can I solve this without string manipulation (deleting milliseconds or adding colon)?
loc_dt = datetime.now(tz)instead.