Convert date and time from UTC to local in specifed format

Solution 1:

what I linked applied specifically to your question:

from datetime import datetime, timezone
from dateutil.tz import gettz

# the given info:
ist = gettz("Asia/Kolkata")
s = "2021-02-24 12:41:40"

# now parse to datetime and set tzinfo to UTC
dt = datetime.fromisoformat(s).replace(tzinfo=timezone.utc)

# convert to IST time zone
dt = dt.astimezone(ist)

# output to isoformat string, but without time zone info
s = dt.replace(tzinfo=None).isoformat(' ')

print(s)
# 2021-02-24 18:11:40

Solution 2:

Solution for Python 3.6.*

        datetime = "2021-02-24 12:41:40"
        from_zone = tz.tzutc()
        to_zone = tz.tzlocal()
        utc = datetime.strptime( datetime,'%Y-%m-%d %H:%M:%S')
        utc = utc.replace(tzinfo=from_zone)
        local = utc.astimezone(to_zone)
        localTime = local.replace(tzinfo=None).isoformat(' ')