Date = dt.datetime.today().date()
I have a date here but it's in UTC, for example at PST 2022-09-18 5PM it will turn into 2022-09-19, but I want to convert it into PST time.
Date = dt.datetime.today().date()
I have a date here but it's in UTC, for example at PST 2022-09-18 5PM it will turn into 2022-09-19, but I want to convert it into PST time.
You need to use a timezone
aware datetime - a naive datetime (like datetime.date
) has no timezone
information.
So, use a datetime.datetime
with a tzinfo
, and then you can localize to UTC and convert to Pacific time:
from datetime import datetime
from dateutil import tz
pst = tz.gettz('America/Los_Angeles')
dt = datetime(2016, 1, 1, tzinfo=tz.UTC)
pst_dt = dt.astimezone(pst)
This gives you a datetime representing 2016-01-01 00:00:00
in the Pacific timezone.
You can of course start out with a naive datetime and use replace to add a UTC timezone:
dt = datetime(2016, 1, 1).replace(tzinfo=tz.UTC)