4

So, I want to convert UTC date time 2021-08-05 10:03:24.585Z to Indian date time how to convert it?

What I tried is

from datetime import datetime

from pytz import timezone

st = "2021-08-05 10:03:24.585Z"

datetime_object = datetime.strptime(st, '%Y-%m-%d %H:%M:%S.%fZ')

local_tz = timezone('Asia/Kolkata')
start_date = local_tz.localize(datetime_object)
print(start_date.replace(tzinfo=local_tz))

But Still the output is not with the timezone I have mentioned how can I convert the time and print the time from the time.

Output:

2021-08-05 10:03:24.585000+05:21
hamere
  • 125
  • 1
  • 3
  • 8
  • @deceze When I tried to print just `start_date` the output is `2021-08-05 10:03:24.585000+05:30` – hamere Aug 05 '21 at 10:17
  • see e.g. [Display the time in a different time zone](https://stackoverflow.com/q/1398674/10197418) and [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/q/127803/10197418) – FObersteiner Aug 05 '21 at 10:17
  • @MrFuppes when I used `astimezone()` it says can't convert naive datetime. I'm a newbie to this you could you spare some of your time and help me by explaining it? – hamere Aug 05 '21 at 10:23
  • yup, the problem is the `Z` in your parsing directive. it basically makes `strptime` ignore the fact that "Z" should be parsed to *aware* datetime, with aware meaning the datetime object "knows" that it belongs in a certain time zone, UTC in this case. – FObersteiner Aug 05 '21 at 10:25

3 Answers3

5

You can use sth like this:

from datetime import datetime
from dateutil import tz

from_zone = tz.gettz('UTC')
to_zone = tz.gettz('Asia/Kolkata')

utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')

utc = utc.replace(tzinfo=from_zone)

central = utc.astimezone(to_zone)
Amin
  • 2,605
  • 2
  • 7
  • 15
4

parse the date/time string to UTC datetime correctly and use astimezone instead of replace to convert to a certain time zone (option for newer Python version (3.9+) in comments):

from datetime import datetime
# from zoneinfo import ZoneInfo
from dateutil.tz import gettz

st = "2021-08-05 10:03:24.585Z"
zone = "Asia/Kolkata"

# dtUTC = datetime.fromisoformat(st.replace('Z', '+00:00'))
dtUTC = datetime.strptime(st, '%Y-%m-%d %H:%M:%S.%f%z')

# dtZone = dtUTC.astimezone(ZoneInfo(zone))
dtZone = dtUTC.astimezone(gettz(zone))

print(dtZone.isoformat(timespec='seconds'))
# 2021-08-05T15:33:24+05:30

If you just need local time, i.e. your machine's setting, you can use astimezone(None) as well.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

In Python 3.10.8

from datetime import datetime
st = "2021-08-05 10:03:24.585Z"
dtUTC = datetime.fromisoformat(st[:-1]+'+00:00')
dtUTC = dtUTC.astimezone()
print(dtUTC.isoformat(timespec='seconds'))
# 2021-08-05T15:33:24+05:30

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • cannot reproduce that this works correctly. my machine is set to UTC+2 so this code gives `2021-08-05T10:03:24+02:00` - which is wrong since the input should be UTC, not UTC+2. – FObersteiner Dec 28 '22 at 13:42