Convert Date and Time from One Time Zone to Another using Python

pytz library

  1. Install pytz library from the command line:
pip install pytz
  1. Convert date and time from one time zone to another:
import datetime import pytz dateTimeFormat = '%Y-%m-%d %H:%M:%S' fromTz = pytz.timezone('Europe/London') toTz = pytz.timezone('America/New_York') fromDateTime = '2020-09-12 20:30:00' dt = datetime.datetime.strptime(fromDateTime, dateTimeFormat) dt = fromTz.localize(dt) dt = dt.astimezone(toTz) toDateTime = dt.strftime(dateTimeFormat) print(toDateTime)

Leave a Comment

Cancel reply

Your email address will not be published.