I have the following datetime, and I converted to datetime object.
How do I convert it to say GMT+7 timezone?
date = "2020-07-27T16:38:20Z"
curr = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z")
print(curr)
I have the following datetime, and I converted to datetime object.
How do I convert it to say GMT+7 timezone?
date = "2020-07-27T16:38:20Z"
curr = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z")
print(curr)
If converting to a timezone will work for you then you can convert it like this:
import datetime as dt
from pytz import timezone
date = "2020-07-27T16:38:20Z"
curr = (
dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z")
.astimezone(timezone('US/Pacific'))
)
print(curr)
OUTPUT:
2020-07-27 09:38:20-07:00
You can list the available timezones like this:
from pytz import all_timezones
for tz in all_timezones:
print(tz)
You can use this code here:
import pytz
date = datetime(2020, 7, 27, 16, 38, 20)
local_time_zone = pytz.timezone('GMT+0')
Or you can get your local time zone
# from tzlocal import get_localzone
# local_time_zone = get_localzone()
def utc_to_local(date):
local_date = date.replace(tzinfo=pytz.utc).astimezone(local_time_zone)
return local_time_zone.normalize(local_date)
print(utc_to_local(date).strftime('%Y-%m-%d %H:%M:%S.%f %Z%z'))
print(utc_to_local(datetime.utcnow()).strftime('%Y-%m-%d %H:%M:%S.%f %Z%z'))
The output will be like this:
2020-07-27 16:38:20.000000 GMT+0000
2020-07-28 13:31:23.219703 GMT+0000