(datetime.utcnow()+datetime.timedelta(hours=8))
This code doesn't seem to work in Python 3 when I'm trying to print my local timezone.
(datetime.utcnow()+datetime.timedelta(hours=8))
This code doesn't seem to work in Python 3 when I'm trying to print my local timezone.
from datetime import datetime
import pytz
tz_NY = pytz.timezone('America/New_York')
datetime_NY = datetime.now(tz_NY)
print("NY time:", datetime_NY.strftime("%H:%M:%S"))
And the easy way is,
from datetime import datetime, timezone
utc_dt = datetime.now(timezone.utc)
print("Local time {}".format(utc_dt.astimezone().isoformat()))
Python docs recommend to use the pytz
module.
pip install pytz
>>> import datetime
>>> import pytz
>>> dt_now = datetime.datetime.now(tz = pytz.UTC)
>>> print(dt_now)
2020-08-09 07:02:20.863890+00:00
Here is the documentation on pytz for more info.