Python · Intermediate Python

Date & Time: Practice Questions

100 questions. Try each one yourself before checking the answer.

Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.

Q1Today & NowEasyMust Do

Get today's date and the current moment, and look at what each one gives you.

Q2Creating DatesEasy

Build a date yourself, and see what happens when the values are impossible.

Q3Date AttributesEasy

Read the parts of a date back out.

Q4Creating DatetimesEasy

Build a datetime with as much or as little precision as you need.

Q5Datetime AttributesEasy

Read every part of a datetime, including the ones a date does not have.

Q6time ObjectsEasy

Use a time object for a clock reading with no date attached.

Q7combine()Easy

Join a date and a time into a datetime, and split one back apart.

Q8Printing DatesEasy

See what str() and repr() give you for each type.

Q9ISO FormatEasyMust Do

Write and read the one date format you should always prefer.

Q10ComparingEasy

Compare and sort dates.

Q11timedeltaEasyMust Do

Create durations with timedelta.

Q12Date ArithmeticEasyMust Do

Add and subtract durations to move around the calendar.

Q13Subtracting DatesEasyMust Do

Find the gap between two dates.

Q14timedelta AttributesEasy

Take a timedelta apart correctly.

Q15weekday()Easy

Find out which day of the week a date falls on.

Q16strftime()EasyMust Do

Format a date for a human to read.

Q17strftime() NamesEasy

Print day and month names instead of numbers.

Q18strptime()EasyMust Do

Turn a date string back into a datetime.

Q19Limits & ResolutionEasy

Find the boundaries of what these types can hold.

Q20replace()EasyMust Do

Change one field of a date by making a modified copy.

Q21TimestampsEasy

Convert between a datetime and a Unix timestamp.

Q22The time ModuleEasy

Use the older time module alongside datetime.

Q23calendar ModuleEasy

Answer calendar questions the datetime module does not.

Q24UTC NowEasy

Get the current time in UTC — the modern way and the deprecated way.

Q25SortingEasy

Sort records by their date.

Q26strftime() DirectivesMediumMust Do

Build a runnable reference for every directive you will actually use.

Q27strptime() FailuresMedium

Understand exactly why a parse fails, and validate a date properly.

Q28Flexible ParsingMedium

Accept a date in any of several formats.

Q29Padding & PortabilityMedium

Remove the leading zeros from a formatted date without breaking on another platform.

Q30ISO 8601 In DepthMedium

Read and write the full ISO format, including offsets.

Q31ISO WeeksMedium

Work with week numbers, and see why there are three different ones.

Q32timedelta ArithmeticMediumMust Do

Do maths with durations themselves.

Q33Formatting DurationsMediumMust Do

Print a duration the way a human would say it.

Q34OrdinalsMedium

Convert a date to a single number and back.

Q35Month ArithmeticMediumMust Do

Add months to a date, given that timedelta cannot.

Q36Month BoundariesMedium

Find the first and last day of a month, quarter and year.

Q37calendar OutputMedium

Print a real calendar.

Q38Business DaysMedium

Count and skip working days.

Q39Age CalculationMedium

Calculate someone's age correctly.

Q40timezone & OffsetsMediumMust Do

Attach a fixed UTC offset to a datetime and see what changes.

Q41zoneinfoMediumMust Do

Use real named time zones, which know about daylight saving.

Q42astimezone()Medium

Convert a moment between time zones without changing the moment.

Q43Daylight SavingMedium

Look at the hour that does not exist and the hour that happens twice.

Q44Measuring TimeMedium

Choose the right clock for measuring how long something took.

Q45Timing ToolsMedium

Build a stopwatch as a context manager and as a decorator.

Q46Rounding TimeMedium

Truncate and round a datetime to a chosen unit.

Q47Date RangesMedium

Generate a series of dates lazily.

Q48Grouping by PeriodMedium

Group dated records into months, weeks and quarters.

Q49Parsing Messy DatesMedium

Pull dates out of free text with a regex, then parse them properly.

Q50Relative TimeMedium

Turn a datetime into "3 hours ago" or "in 2 days".

Q51SchedulingMediumMust Do

Find a meeting slot that works for a team spread across time zones.

Q52DeadlinesMedium

Track deadlines and report what is overdue, due soon and on track.

Q53AttendanceMedium

Turn raw punch-in and punch-out stamps into an attendance register.

Q54Log WindowsMediumMust Do

Filter a log to a time window, and summarise what happened in it.

Q55Recurring EventsMedium

Generate a recurring schedule: daily, weekly, monthly.

Q56CountdownMedium

Build a countdown to an event with a progress bar.

Q57Working HoursMedium

Measure elapsed time counting only office hours.

Q58Billing PeriodsMediumMust Do

Work out subscription renewal dates and a prorated charge.

Q59RemindersMedium

Build a birthday reminder list that handles the year rollover.

Q60Storing DatesMedium

Write dates to CSV and JSON, and read them back exactly.

Q61Time BucketingMedium

Build an hourly histogram of events.

Q62Duration StatisticsMedium

Summarise a set of durations properly.

Q63Calendar ViewsMedium

Print a month calendar with events marked on it.

Q64TimesheetsMediumMust Do

Total a week of HH:MM entries and work out overtime.

Q65CutoffsMedium

Work out a delivery date from an order time and a daily cutoff.

Q66Mixed ZonesMedium

Sort and display a feed of events submitted from different time zones.

Q67CooldownsMedium

Implement a rate limiter with a sliding window.

Q68SessionsMedium

Split a stream of activity into sessions using an inactivity timeout.

Q69Fiscal PeriodsMediumMust Do

Map calendar dates onto a financial year that does not start in January.

Q70BookingMedium

Find free slots in a day given a list of existing bookings.

Q71DebuggingHardMust Do

This function crashes on some inputs and not others. Explain why and fix it.

from datetime import datetime, timezone
 
def hours_until(deadline):
    return (deadline - datetime.now()).total_seconds() / 3600
 
print(hours_until(datetime(2030, 1, 1)))
print(hours_until(datetime(2030, 1, 1, tzinfo=timezone.utc)))
Q72utcnow()Hard

Show the silent bug that datetime.utcnow() causes.

Q73DST ArithmeticHardMust Do

Show that "add one day" and "add 24 hours" are different answers.

Q74timedelta FieldsHard

Show the bug caused by reading .seconds instead of .total_seconds().

Q75Leap YearsHard

Handle 29 February and the century rule.

Q76Two-Digit YearsHard

See where %y decides a two-digit year belongs.

Q77Sorting StringsHard

Show why sorting dates as strings works in exactly one format.

Q78PrecisionHard

Watch precision disappear when datetimes become floats.

Q79replace() TrapsHard

Show where .replace() raises, and what to use instead.

Q80Timestamps & ZonesHardMust Do

Show how a timestamp goes wrong when the timezone is left out.

Q81ISO Week YearHard

Show the week-numbering bug that appears once a year.

Q82Clock ChoiceHard

Show why time.time() is the wrong clock for a timeout.

Q83DebuggingHard

This holiday calculator reports the wrong number of days. Find the off-by-one.

from datetime import date
 
def holiday_days(first_day, last_day):
    return (last_day - first_day).days
 
print(holiday_days(date(2024, 8, 12), date(2024, 8, 16)))
print(holiday_days(date(2024, 8, 12), date(2024, 8, 12)))
Q84Default ArgumentsHard

Show what happens when date.today() is used as a default argument.

Q85Locale & PlatformHard

Show which parts of date formatting are not portable.

Q86Mini-ProjectMini-Project

Build an Attendance & Payroll Report from raw shift records.

Q87Mini-ProjectMini-Project

Build a Project Schedule Planner with dependencies and a text Gantt chart.

Q88Mini-ProjectMini-ProjectMust Do

Build a World Clock Dashboard showing one instant everywhere.

Q89Mini-ProjectMini-Project

Build a Log Analytics Report with time bucketing and incident detection.

Q90Mini-ProjectMini-Project

Build a Recurring Event Engine driven by rules.

Q91Mini-ProjectMini-Project

Build a Flight Itinerary calculator across time zones.

Q92Mini-ProjectMini-Project

Build a Habit Tracker with streaks and a heat map.

Q93Mini-ProjectMini-Project

Build an Invoice Ageing Report with buckets and interest.

Q94Mini-ProjectMini-ProjectMust Do

Build a Server Uptime Analyser from a stream of status changes.

Q95Mini-ProjectMini-Project

Build a Personal Calendar with conflict detection and a free/busy view.

Q96InterviewInterview

Explain naive versus aware datetimes completely, with every rule that follows from it.

Q97InterviewInterview

Map the whole datetime API: what each call takes and what it gives back.

Q98InterviewInterview

State the rules for storing and exchanging time, and demonstrate what each one prevents.

Q99InterviewInterview

Catalogue the failure modes of date handling, with a demonstration of each.

Q100CapstoneInterviewMust Do

Build a Global Event Platform — the complete demonstration of this topic. Ingest events in mixed formats and zones, validate them, expand recurring rules, detect conflicts, and produce a per-viewer schedule with a full report.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session