So, "2021-08-17T20:03:36.480-07:00", is given as as a string. I want to convert it into the local time.
Something like this 2020-01-06 00:00:00.
This is what I had tried earlier
from datetime import datetime
def convert_utc_local(utc_time):
conv_time = ' '.join(utc_time.split("T"))
return conv_time.astimezone(tzlocal())
And the error came to
return utc_time.astimezone(tzlocal())
AttributeError: 'str' object has no attribute 'astimezone'"
So what worked for me was this:
def convert_utc_local(utc_time):
to_zone = tz.gettz('Asia/Tokyo')
iso_to_local = datetime.fromisoformat(utc_time).astimezone(to_zone)
dt = str(iso_to_local.replace(tzinfo=None).isoformat(' ', timespec='seconds'))
return dt