Unix timestamp to iso 8601 time format

23,066

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
Share:
23,066
kotlavaibhav
Author by

kotlavaibhav

Updated on July 10, 2022

Comments

  • kotlavaibhav
    kotlavaibhav almost 2 years

    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
    FObersteiner over 2 years
    With Python 3.9+, you can use the standard library 's zoneinfo module to get the timezone object.