0
(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.

1

2 Answers 2

0
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())) 
Sign up to request clarification or add additional context in comments.

2 Comments

@PhillipDev, If you feel that above answer helped you, you could accept my answer.
Accepted your answer!
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.