kotlavaibhav
kotlavaibhav

Reputation: 239

Unix timestamp to iso 8601 time format

When I convert unix time 1463288494 to isoformat i get 2016-05-14T22:01:34. How can I get the output including the -07:00. In this format 2016-05-14T22:01:34-07:00

from datetime import datetime
t =  int("1463288494")
print(datetime.fromtimestamp(t).isoformat())

Upvotes: 23

Views: 39914

Answers (1)

mhawke
mhawke

Reputation: 87054

You can pass a tzinfo instance representing your timezone offset to fromtimestamp(). The problem then is how to get the tzinfo object. The easiest way is to use the pytz module which provides a tzinfo compatible object:

import pytz
from datetime import datetime

tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(1463288494, tz).isoformat())

#2016-05-14T22:01:34-07:00

Upvotes: 27

Related Questions