Created
September 19, 2019 22:33
-
-
Save gwsu2008/940be99cf7af0c1c48b2ec2c6d085e99 to your computer and use it in GitHub Desktop.
python_convert_utc_to_local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from dateutil import tz | |
# METHOD 1: Hardcode zones: | |
from_zone = tz.gettz('UTC') | |
to_zone = tz.gettz('America/New_York') | |
# METHOD 2: Auto-detect zones: | |
from_zone = tz.tzutc() | |
to_zone = tz.tzlocal() | |
# utc = datetime.utcnow() | |
utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S') | |
# Tell the datetime object that it's in UTC time zone since | |
# datetime objects are 'naive' by default | |
utc = utc.replace(tzinfo=from_zone) | |
# Convert time zone | |
central = utc.astimezone(to_zone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment