19

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())
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
kotlavaibhav
  • 199
  • 1
  • 2
  • 8

1 Answers1

23

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
mhawke
  • 84,695
  • 9
  • 117
  • 138