Dealing with Timezones in Python Like a Pro


Handling dates and times in Python sounds simple — until you need to support multiple timezones, daylight saving time, or convert between UTC and local time. Then things get tricky. 😅
This post will walk you through:
Common datetime pitfalls
How to properly use
datetime
,pytz
, andzoneinfo
Real-world examples of timezone-aware code
🧠 The Problem
Here’s a common beginner mistake:
pythonCopyEditfrom datetime import datetime
now = datetime.now()
print(now)
This prints your local time, but it's naive — it doesn’t include any timezone information.
Now try this:
pythonCopyEditfrom datetime import datetime, timezone
utc_now = datetime.now(timezone.utc)
print(utc_now)
This one is timezone-aware — and that makes a huge difference.
📍 Why Timezones Matter
Let’s say your app:
Stores timestamps in UTC (best practice)
Needs to show them in a user’s local timezone (e.g., New York, Tokyo)
Must handle daylight saving time correctly
You can’t do that reliably with naive datetimes.
✅ The Right Way (Python 3.9+)
Python 3.9+ includes the built-in zoneinfo
module. No need for external libraries!
pythonCopyEditfrom datetime import datetime
from zoneinfo import ZoneInfo
utc_time = datetime(2024, 11, 3, 1, 30, tzinfo=ZoneInfo("UTC"))
ny_time = utc_time.astimezone(ZoneInfo("America/New_York"))
print("UTC:", utc_time)
print("New York:", ny_time)
This handles DST transitions correctly, even the awkward hour where clocks roll back.
🧾 Formatting for Humans
When displaying dates to users:
pythonCopyEditprint(ny_time.strftime("%Y-%m-%d %H:%M:%S %Z"))
# Output: 2024-11-02 21:30:00 EDT
Always include the time zone abbreviation (%Z
) to avoid confusion.
🔁 Converting Between Timezones
pythonCopyEditdt = datetime(2025, 7, 21, 14, 0, tzinfo=ZoneInfo("Asia/Shanghai"))
la_time = dt.astimezone(ZoneInfo("America/Los_Angeles"))
print("Shanghai:", dt)
print("Los Angeles:", la_time)
This is perfect for scheduling apps, dashboards, or chat logs.
🕳️ Gotchas to Avoid
❌ Mixing naive and aware datetimes (raises
TypeError
)❌ Assuming all timezones are offset-only (DST breaks this)
❌ Parsing string timestamps without including tz info
💡 Bonus: For Older Python Versions
If you're on Python <3.9, you can use pytz
:
pythonCopyEditfrom datetime import datetime
import pytz
utc = pytz.UTC
dt = utc.localize(datetime.utcnow())
local = dt.astimezone(pytz.timezone('Asia/Kolkata'))
print(local)
But zoneinfo
is recommended for future projects — it's simpler and more accurate.
🧵 Wrapping Up
Timezones can be messy, but Python gives us powerful tools to handle them cleanly. With datetime
, zoneinfo
, and a little discipline, your apps can work globally without confusing your users.
🌐 Working on something that needs time zone logic? Share it in the comments — let's swap war stories!
Subscribe to my newsletter
Read articles from Ashraful Islam Leon directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ashraful Islam Leon
Ashraful Islam Leon
Passionate Software Developer | Crafting clean code and elegant solutions