Display the time in a different time zone

Adam Matan picture Adam Matan · Sep 9, 2009 · Viewed 88.4k times · Source

Is there an elegant way to display the current time in another time zone?

I would like to have something with the general spirit of:

cur = <Get the current time, perhaps datetime.datetime.now()>
print("Local time   {}".format(cur))
print("Pacific time {}".format(<something like cur.tz('PST')>))
print("Israeli time {}".format(<something like cur.tz('IST')>))

Answer

Mark Theunissen picture Mark Theunissen · Feb 23, 2011

A simpler method:

from datetime import datetime
from pytz import timezone    

south_africa = timezone('Africa/Johannesburg')
sa_time = datetime.now(south_africa)
print sa_time.strftime('%Y-%m-%d_%H-%M-%S')