Date & Time — Practice Questions
50 questions. Try each one yourself before checking the answer.
Import datetime and print today's date using datetime.date.today().
Print the current date AND time using datetime.datetime.now().
Create a specific date, June 15, 2024, using datetime.date(year, month, day).
Create a specific datetime, June 15, 2024 at 14:30:00, using datetime.datetime(year, month, day, hour, minute, second).
Given a date, print its .year, .month, and .day separately.
Given a datetime, print its .hour, .minute, and .second separately.
Given a date, print .weekday() (0=Monday) and explain in a comment what the returned number means.
Create both a datetime.date and a datetime.datetime for the same day, and print their type()s to show they're distinct classes.
Convert today's date to a string using str() and print its type() before and after.
Create two dates and print whether the first is earlier than the second using <.
Create a timedelta of 7 days and ADD it to today's date to find the date one week from now.
Subtract two dates to get a timedelta, then print how many days apart they are.
Create a timedelta of days=2, hours=5, minutes=30, and print its total number of seconds using .total_seconds().
Format today's date as "DD/MM/YYYY" using .strftime().
Parse the string "15-06-2024" into a date object using datetime.datetime.strptime().
Format today's date to show the FULL weekday and month name (e.g. "Saturday, June 15, 2024") using %A and %B.
Create two datetime objects and print whether the first happened BEFORE the second, using <.
Create a timedelta using weeks=2 and separately one using hours=48, and compare whether they represent the same duration in DAYS.
Given a birth date, calculate the person's age in YEARS (approximately, using day counting).
Given a deadline date, print whether it's in the PAST or the FUTURE relative to today.
Build a "Countdown" that computes and prints how many days remain until a specific future event date.
Given two dates, print how many WEEKS apart they are (whole weeks, using integer division on the day difference).
Ask the user to enter a date as "DD-MM-YYYY", parse it with strptime(), and print it back formatted as "Month DD, YYYY".
Compute a project deadline that's exactly 30 days from today, and print it formatted.
Given a birth date, calculate the person's age precisely (accounting for whether their birthday has already occurred THIS year), using month/day comparison.
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).
Print the current time formatted as "HH:MM:SS" using datetime.datetime.now().strftime().
Convert the current datetime to a Unix TIMESTAMP using .timestamp(), then convert it back to a datetime using datetime.datetime.fromtimestamp().
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)Format a datetime as a full timestamp string like "2024-06-15 14:30:00" using %Y-%m-%d %H:%M:%S.
Create a timezone-AWARE datetime in UTC using datetime.timezone.utc, and show the difference in repr() compared to a naive one.
Create a UTC-aware datetime, then convert it to a fixed UTC+5:30 offset (e.g. India) using .astimezone().
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.
Given two datetimes representing a meeting's start and end, compute the duration and format it as "Xh Ym".
Use calendar.monthrange(year, month) to find out how many days are in a given month, and what weekday it starts on.
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().
Given a datetime, use .replace() to reset its time portion to exactly midnight (00:00:00) while keeping the date unchanged.
Given a list of unsorted date STRINGS in "DD-MM-YYYY" format, parse and sort them chronologically, then print them back as formatted strings.
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.
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.
Build a "Countdown Timer". Ask the user for a target date, and print how many days, and whether it's already passed.
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.
Build a simple "Appointment Scheduler" that checks whether a new appointment time conflicts with an existing list of booked (start, end) datetime pairs.
Build a "Meeting Duration Calculator". Ask for a start and end time (HH:MM), and print the meeting length formatted as "Xh Ym".
Build a "Deadline Tracker" managing a list of (task, deadline) pairs, printing each task's status: "X days left" or "Overdue by X days".
Explain the difference between "naive" and "timezone-aware" datetimes, and demonstrate why comparing a naive datetime to an aware one raises a TypeError.
Explain the industry best practice of storing all timestamps in UTC internally and only converting to a local timezone for DISPLAY, demonstrating the pattern.
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.
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).
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