1

I have a string which represents datetime with timezone, I am trying to convert it to Indian Standard Time (IST) by parsing the string.

Date string is,

"2018-12-24T02:35:16-08:00"

how can I know what's the actual time as per IST ?

aakash singh
  • 267
  • 5
  • 19

1 Answers1

2
from datetime import datetime
from dateutil import tz
utc_tz= tz.gettz('UTC')
india_tz= tz.gettz('Asia/Kolkata')
date_string="2018-12-24T02:35:16-08:00"
#utc = datetime.strptime(date_string[:date_string.rindex('-')].replace('T',' '), '%Y-%m-%d %H:%M:%S')
# or
utc = datetime.strptime(date_string[:date_string.rindex('-')], '%Y-%m-%dT%H:%M:%S')
utc = utc.replace(tzinfo=utc_tz)
india_time_with_offset = utc.astimezone(india_tz)
india_time_without_offset = india_time_with_offset.replace(tzinfo=None)
print(india_time_with_offset)
print(india_time_without_offset)

Output

2018-12-24 08:05:16+05:30
2018-12-24 08:05:16
Bitto
  • 7,937
  • 1
  • 16
  • 38