0

I have a variable that stores a date taken from a JSON file: enter image description here

I have to change the time zone to what is currently in Thailand. Simply put, add 6 hours to the current time with a possible change of date. How can I easily change the time zone? I tried to use datetime library for this, but it did not bring any results.

gniecol3
  • 13
  • 5

2 Answers2

0

Use datetime.strptime to convert it to a datetime object and then add the offset hours using timedelta.

Raj
  • 664
  • 4
  • 16
0

Something like this:

from datetime import datetime, timezone, timedelta

d = datetime.fromisoformat("2019-12-18T15:20:58.950911+01:00")
print(d)
d2 = d.astimezone(timezone(timedelta(hours=6)))
print(d2)

Output:

2019-12-18 15:20:58.950911+01:00
2019-12-18 20:20:58.950911+06:00

UPDATE: If using python < 3.7, fromisoformat isn't avaiable, so you should use strptime instead, see e.g.: https://stackoverflow.com/a/10805633/2495746

pjoe
  • 186
  • 1
  • 7