How to do Timezone conversion with Python?

Sometimes, we want to do Timezone conversion with Python.

In this article, we’ll look at how to do Timezone conversion with Python.

How to do Timezone conversion with Python?

To do Timezone conversion with Python, we can use the pytz module.

For instance, we write

import datetime
import pytz

dt_today = datetime.datetime.today()
dt_london = dt_today.astimezone(pytz.timezone('Europe/London'))

to call datetime.datetime.today to get today’s datetime.

Then we convert the datetime to the London time zone with the astimezone method called with the time zone returned by pytz.timezone called with the time zone string.

Conclusion

To do Timezone conversion with Python, we can use the pytz module.