I am trying the following:
import datetime
from pytz import timezone
date = datetime.datetime(2011,01,01) # this is in UTC time
tz = timezone('US/Pacific')
How would I then convert this datetime into the US/Pacific equivalent?
Its been asked before, but any way you can find it here -
http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/
If you know the hour difference to which you would like to change you can do following -
e.g. - your timezone is +2 hours to UTC
from datetime import datetime, timedelta
current_utc = datetime.utcnow()
my_timezone = current_utc + timedelta(hours=2)
You can provide the tzinfo
argument to the datetime
function:
tz = timezone('US/Pacific')
date = datetime.datetime(2011, 01, 01, tzinfo=tz)
Or convert an existing datetime object to another timezone:
date_pacific = date.astimezone(timezone('US/Pacific'))