Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I print the time in a specific timezone in Python?

This question is really quite simple, and it's basically the title. I am in Western Australia.

like image 853
Nick Lai Avatar asked Sep 03 '25 16:09

Nick Lai


1 Answers

Yup the pytz and datetime module is the way to go:

import pytz
from datetime import datetime

timezone = 'Australia/Perth'
py_timezone = pytz.timezone(timezone)
current_datetime = datetime.now(py_timezone)

If you need to find a specific timezone you use the all_timezones property in pytz, which will return a list of all timezones.

import pytz
all_timezones = pytz.all_timezones
like image 127
Eugene Upston Avatar answered Sep 05 '25 04:09

Eugene Upston