datetime Interview Questions & Answers

5 questions Updated 2026-06-18

Python interview questions on datetime vs date vs time, naive vs timezone-aware datetimes with zoneinfo, strftime/strptime, timedelta arithmetic, and the now() vs utcnow() pitfall.

The datetime module provides three core types. date holds a calendar date (year, month, day) with no time. time holds a time of day (hour, minute, second, microsecond) with no date. datetime combines both into a single timestamp.

from datetime import date, time, datetime

d = date(2026, 6, 18)               # just the date
t = time(14, 30, 0)                 # just the time of day
dt = datetime(2026, 6, 18, 14, 30)  # date + time together

dt.date()                           # -> date(2026, 6, 18)
dt.time()                           # -> time(14, 30)
date.today()                        # current date

Use date for things like birthdays or due dates where time is irrelevant, time for a recurring clock time, and datetime for actual events/timestamps. Most real-world work uses datetime.

A naive datetime has no timezone info (tzinfo is None) — it's just wall clock numbers with no reference point, so it's ambiguous. An aware datetime carries a tzinfo, pinning it to an actual instant. Use the stdlib zoneinfo module (Python 3.9+) to attach real IANA timezones.

from datetime import datetime
from zoneinfo import ZoneInfo

naive = datetime(2026, 6, 18, 14, 30)          # ambiguous — no tz
aware = datetime(2026, 6, 18, 14, 30,
                 tzinfo=ZoneInfo("America/New_York"))

utc = aware.astimezone(ZoneInfo("UTC"))        # convert between zones

You can't compare or subtract a naive and an aware datetime — it raises TypeError. Best practice: store and compute in UTC-aware datetimes, and convert to local zones only for display.

They are inverses. strftime ("string from time") formats a datetime into a string using format codes. strptime ("string parse time") parses a string into a datetime using a matching format.

from datetime import datetime

dt = datetime(2026, 6, 18, 14, 30)
s = dt.strftime("%Y-%m-%d %H:%M")       # datetime -> "2026-06-18 14:30"

parsed = datetime.strptime("2026-06-18 14:30",
                           "%Y-%m-%d %H:%M")  # str -> datetime

Common codes: %Y (4-digit year), %m (month), %d (day), %H (24-hour), %M (minute), %S (second). To remember: f = format (out), p = parse (in). For standard ISO strings, datetime.fromisoformat() / .isoformat() are simpler.

A timedelta represents a duration — a difference between two points in time. Subtracting two datetimes yields a timedelta; adding a timedelta to a datetime shifts it. A timedelta stores days, seconds, and microseconds.

from datetime import datetime, timedelta

start = datetime(2026, 6, 18, 9, 0)
end   = datetime(2026, 6, 18, 17, 30)

worked = end - start              # timedelta(seconds=30600)
worked.total_seconds()            # 30600.0
worked.seconds // 3600            # 8 (hours portion)

tomorrow = start + timedelta(days=1)      # shift forward
week_ago = start - timedelta(weeks=1)     # shift back

Use total_seconds() to get the whole duration as a number (the .seconds attribute is only the sub-day part). timedelta makes date math safe — it correctly rolls over months and years.

The big trap: both datetime.now() and the old datetime.utcnow() return naive datetimes. now() gives local wall time, utcnow() gives the UTC wall time — but neither attaches a tzinfo, so a utcnow() value silently looks like local time and corrupts later conversions. utcnow() is deprecated in modern Python.

from datetime import datetime
from zoneinfo import ZoneInfo

datetime.now()                       # naive, local time — ambiguous
datetime.utcnow()                    # naive, but labelled nothing! (deprecated)

# correct: an AWARE UTC timestamp
now_utc = datetime.now(ZoneInfo("UTC"))
local = datetime.now(ZoneInfo("America/New_York"))

Rule of thumb: always pass a timezone to now() to get an aware datetime, and avoid utcnow() entirely. Store timestamps as UTC-aware and convert for display.

Practice tests are coming soon

Get notified when interactive mock interviews and quizzes launch.