1

I need to convert an arbitrary datetime string for example "2015-10-01 12:20:00 UTC" and convert it to another timezone for example "Europe/Istanbul" with using a simple code in python

I read these answers/questions:

Python Timezone conversion

Python - Convert UTC datetime string to local datetime

but they are not clear and all of them convert nowtime but I need convert any time in any timezone to another arbitrary zonetime

Community
  • 1
  • 1
Ali
  • 602
  • 6
  • 18

1 Answers1

2

You can do it easily with modules datetime and dateutil:

from datetime import datetime
from dateutil import tz

dobj = datetime.strptime("2015-10-01 12:20:00", '%Y-%m-%d %H:%M:%S')
dobj = dobj.replace(tzinfo=tz.gettz("UTC"))

print(dobj)
print(dobj.astimezone(tz.gettz("Europe/Istanbul")))

Output:

$ python2.7 time.py 
2015-10-01 12:20:00+00:00
2015-10-01 15:20:00+03:00
Alberto Re
  • 514
  • 3
  • 9