Python · Intermediate Python

Date & Time — Practice Questions

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

Q1datetimeEasy

Import datetime and print today's date using datetime.date.today().

Q2datetimeEasy

Print the current date AND time using datetime.datetime.now().

Q3Creating DatesEasy

Create a specific date, June 15, 2024, using datetime.date(year, month, day).

Q4Creating DatetimesEasy

Create a specific datetime, June 15, 2024 at 14:30:00, using datetime.datetime(year, month, day, hour, minute, second).

Q5Date AttributesEasy

Given a date, print its .year, .month, and .day separately.

Q6Datetime AttributesEasy

Given a datetime, print its .hour, .minute, and .second separately.

Q7weekday()Easy

Given a date, print .weekday() (0=Monday) and explain in a comment what the returned number means.

Q8date vs datetimeEasy

Create both a datetime.date and a datetime.datetime for the same day, and print their type()s to show they're distinct classes.

Q9str() ConversionEasy

Convert today's date to a string using str() and print its type() before and after.

Q10Comparing DatesEasy

Create two dates and print whether the first is earlier than the second using <.

Q11timedeltaMedium

Create a timedelta of 7 days and ADD it to today's date to find the date one week from now.

Q12timedeltaMedium

Subtract two dates to get a timedelta, then print how many days apart they are.

Q13timedelta ComponentsMedium

Create a timedelta of days=2, hours=5, minutes=30, and print its total number of seconds using .total_seconds().

Q14strftime()Medium

Format today's date as "DD/MM/YYYY" using .strftime().

Q15strptime()Medium

Parse the string "15-06-2024" into a date object using datetime.datetime.strptime().

Q16strftime()Medium

Format today's date to show the FULL weekday and month name (e.g. "Saturday, June 15, 2024") using %A and %B.

Q17Comparing DatetimesMedium

Create two datetime objects and print whether the first happened BEFORE the second, using <.

Q18timedeltaMedium

Create a timedelta using weeks=2 and separately one using hours=48, and compare whether they represent the same duration in DAYS.

Q19Real-WorldMedium

Given a birth date, calculate the person's age in YEARS (approximately, using day counting).

Q20Real-WorldMedium

Given a deadline date, print whether it's in the PAST or the FUTURE relative to today.

Q21Real-WorldMedium

Build a "Countdown" that computes and prints how many days remain until a specific future event date.

Q22Real-WorldMedium

Given two dates, print how many WEEKS apart they are (whole weeks, using integer division on the day difference).

Q23Real-WorldMedium

Ask the user to enter a date as "DD-MM-YYYY", parse it with strptime(), and print it back formatted as "Month DD, YYYY".

Q24Real-WorldMedium

Compute a project deadline that's exactly 30 days from today, and print it formatted.

Q25Real-WorldMedium

Given a birth date, calculate the person's age precisely (accounting for whether their birthday has already occurred THIS year), using month/day comparison.

Q26Real-WorldMedium

Given a start date, print the date that is exactly 5 WEEKDAYS later (a simple version that just skips Saturday/Sunday, one day at a time).

Q27Real-WorldMedium

Print the current time formatted as "HH:MM:SS" using datetime.datetime.now().strftime().

Q28TimestampsMedium

Convert the current datetime to a Unix TIMESTAMP using .timestamp(), then convert it back to a datetime using datetime.datetime.fromtimestamp().

Q29DebuggingMedium

The following code crashes with a ValueError. Find and fix the bug.

import datetime
 
date_string = "15-06-2024"
parsed = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print(parsed)
Q30FormattingMedium

Format a datetime as a full timestamp string like "2024-06-15 14:30:00" using %Y-%m-%d %H:%M:%S.

Q31Timezone-Aware DatetimesHard

Create a timezone-AWARE datetime in UTC using datetime.timezone.utc, and show the difference in repr() compared to a naive one.

Q32Timezone ConversionHard

Create a UTC-aware datetime, then convert it to a fixed UTC+5:30 offset (e.g. India) using .astimezone().

Q33Date Arithmetic Edge CasesHard

Demonstrate that adding a timedelta across a MONTH boundary (e.g. Jan 31 + 1 day) correctly rolls over to the next month, and that adding across a LEAP YEAR February works correctly too.

Q34Duration FormattingHard

Given two datetimes representing a meeting's start and end, compute the duration and format it as "Xh Ym".

Q35calendar ModuleHard

Use calendar.monthrange(year, month) to find out how many days are in a given month, and what weekday it starts on.

Q36ImmutabilityHard

Demonstrate that datetime objects are IMMUTABLE (trying date.year = 2025 raises an AttributeError), and show the correct way to get a modified copy using .replace().

Q37replace()Hard

Given a datetime, use .replace() to reset its time portion to exactly midnight (00:00:00) while keeping the date unchanged.

Q38Sorting DatesHard

Given a list of unsorted date STRINGS in "DD-MM-YYYY" format, parse and sort them chronologically, then print them back as formatted strings.

Q39Round-Trip FormattingHard

Format a datetime to a string with strftime(), then parse that SAME string back with strptime() using the matching format, and confirm the round-trip produces an equal object.

Q40Next WeekdayHard

Given today's date, compute the date of the NEXT Monday (if today already is Monday, roll forward to the following one), using .weekday() and timedelta.

Q41Mini-ProjectMini-Project

Build a "Countdown Timer". Ask the user for a target date, and print how many days, and whether it's already passed.

Q42Mini-ProjectMini-Project

Build an "Age Calculator". Ask for a birth date and print the exact age in years, accounting for whether the birthday has occurred yet this year.

Q43Mini-ProjectMini-Project

Build a simple "Appointment Scheduler" that checks whether a new appointment time conflicts with an existing list of booked (start, end) datetime pairs.

Q44Mini-ProjectMini-Project

Build a "Meeting Duration Calculator". Ask for a start and end time (HH:MM), and print the meeting length formatted as "Xh Ym".

Q45Mini-ProjectMini-Project

Build a "Deadline Tracker" managing a list of (task, deadline) pairs, printing each task's status: "X days left" or "Overdue by X days".

Q46InterviewInterview

Explain the difference between "naive" and "timezone-aware" datetimes, and demonstrate why comparing a naive datetime to an aware one raises a TypeError.

Q47InterviewInterview

Explain the industry best practice of storing all timestamps in UTC internally and only converting to a local timezone for DISPLAY, demonstrating the pattern.

Q48InterviewInterview

Explain why datetime objects are immutable and why that's actually a FEATURE (not just a limitation), tying it back to the general immutability discussion from the Tuples module.

Q49InterviewInterview

Explain why timedelta has no direct "months" or "years" unit, demonstrating the ambiguity that would result (a month can be 28-31 days) and the common workaround using dateutil.relativedelta (or manual year/month math).

Q50CapstoneInterview

Build a "Date & Time Mastery Report" — the most complete demonstration in this module. Given a birth date and an event date (both parsed from strings), print: current age, days until the event, the event's formatted full weekday/date, and the total duration between the birth date and the event in years.

Still stuck on something?

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

Book a Free Session