Stop 08 · Intermediate PythonTopics · 9/10

Dates & Times (datetime, zoneinfo)

Part of the Python Roadmap

In short

Due dates, delivery slots, meeting reminders across time zones: dates show up in almost every real app, and they're surprisingly easy to get wrong. datetime and zoneinfo handle the hard parts for you.

datetime represents dates, times and durations (timedelta), strftime and strptime convert them to and from text, and zoneinfo handles time zones such as Asia/Kolkata correctly.

The jaadu stands in an airport hall looking up at a row of round wall clocks, each showing a different hour.

Think of it like this

The clocks at an airport

Big airports show a row of clocks: New Delhi, London, New York. It's the same moment everywhere, just shown differently. A timezone-aware datetime is that moment, and zoneinfo is how you read it off any city's clock without doing maths in your head.

Watch it happen

Scheduling a call from Bengaluru with a client in London

Parse text
Attach zone
Convert
Format

1/4strptime turns '2026-12-10 18:30' into a datetime. It knows no time zone yet (naive).

The big ideas

What you need to know

date, time, datetime

date for a calendar day, datetime for a day plus time. datetime.now() gives the current moment.

timedelta for gaps

Add or subtract timedelta(days=7, hours=2). Subtracting two datetimes gives a timedelta.

strftime and strptime

strftime formats a datetime as text (f for format). strptime parses text into a datetime (p for parse).

Always be aware

Use ZoneInfo('Asia/Kolkata') for real times. Store times in UTC and convert only when showing them to users.

See it in code

Walk through the code

python
1from datetime import date, datetime, timedelta, timezone2from zoneinfo import ZoneInfo3 4IST = ZoneInfo("Asia/Kolkata")5now = datetime.now(IST)6print(now.strftime("%d-%m-%Y %H:%M"))7 8due = date(2026, 12, 31)9print("Days left:", (due - date.today()).days)10print("Reminder on:", due - timedelta(days=3))11 12meeting = datetime.strptime("2026-12-10 18:30", "%Y-%m-%d %H:%M")13meeting = meeting.replace(tzinfo=IST)14london = meeting.astimezone(ZoneInfo("Europe/London"))15print(london.strftime("%d %b, %I:%M %p"))   # 10 Dec, 01:00 PM16 17stored = meeting.astimezone(timezone.utc)18print(stored.isoformat())                   # 2026-12-10T13:00:00+00:0019 20start = datetime(2026, 12, 1, 9, 0, tzinfo=IST)21end = datetime(2026, 12, 1, 17, 45, tzinfo=IST)22hours = (end - start).total_seconds() / 360023print(f"Worked {hours:.2f} hours")          # Worked 8.75 hours

1/6Pass a zone to now() to get an aware datetime, then format it Indian style.

Current IST time, a countdown, converting a meeting to London time, storing in UTC and measuring a work shift.

Save yourself the pain

Common mistakes

Oops

Using datetime.now() with no zone on a server and assuming it's Indian time.

Do this instead

Servers often run in UTC. Use datetime.now(ZoneInfo('Asia/Kolkata')) or store UTC and convert.

Oops

Comparing a naive datetime with an aware one and getting TypeError.

Do this instead

Make both aware, usually by attaching a ZoneInfo, before comparing or subtracting.

Oops

Mixing up %m (month) and %M (minute) in format strings.

Do this instead

Lowercase %m is month, uppercase %M is minute. %H is 24-hour, %I is 12-hour with %p for AM/PM.

Your mission

Delivery slot calculator

~25 min

Quick check

Which method turns the text '15/08/2026' into a datetime object?

Go deeper

  1. 1Calculate how many days are left until your next exam.
  2. 2Parse '25-12-2025' into a date with strptime and format it differently.
  3. 3Create a timezone-aware datetime for Asia/Kolkata with zoneinfo.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge