Answer key · 12 questions

Python Dates and Times with datetime Exercises With Answers

All 12 questions for Python Dates and Times with datetime in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Dates and Times with datetime, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does date(2026, 1, 1).isoformat() return?

  1. 2026-01-01correct
  2. January 1, 2026
  3. 2026/1/1
  4. 01/01/2026

Answer: 2026-01-01

Why: isoformat uses the sortable YYYY-MM-DD representation.

02 · Multiple choice

What does adding a timedelta to a date return?

  1. A string
  2. A new date objectcorrect
  3. The same date changed in place
  4. A timezone

Answer: A new date object

Why: Date objects are immutable, so addition returns a new date.

03 · Multiple choice

How do you get the number of days in a timedelta?

  1. delta.dayscorrect
  2. delta.total
  3. len(delta)
  4. delta.days()

Answer: delta.days

Why: days is an attribute, not a method.

04 · Multiple choice

Which strftime pattern gives a four-digit year?

  1. %y
  2. %d
  3. %Ycorrect
  4. %m

Answer: %Y

Why: %Y is the four-digit year; %y is two digits.

05 · Fill in the blank

Complete the method that returns YYYY-MM-DD.

from datetime import date
print(date(2026, 1, 1).____())

Answer: isoformat

Why: isoformat returns the standard ISO date string.

06 · Fill in the blank

Complete the duration class.

from datetime import date, timedelta
print(date(2026, 1, 1) + ____(days=2))

Answer: timedelta

Why: timedelta represents a difference between dates or times.

07 · Fill in the blank

Complete the attribute that gives the day count.

from datetime import date
print((date(2026, 1, 3) - date(2026, 1, 1)).____)

Answer: days

Why: Subtracting dates returns a timedelta with a days attribute.

08 · Fill in the blank

Complete the format pattern for a four-digit year.

from datetime import date
print(date(2026, 10, 27).strftime("____/%m/%d"))

Answer: %Y

Why: A percent sign and capital Y represent the year.

09 · Predict output

What is the output?

from datetime import date
print(date(2026, 1, 2).isoformat())

Answer: 2026-01-02

Why: isoformat returns the date as YYYY-MM-DD.

10 · Predict output

What does this code print?

from datetime import date
print((date(2026, 1, 3) - date(2026, 1, 1)).days)

Answer: 2

Why: The difference is two days.

11 · Predict output

What does the program output?

from datetime import date, timedelta
print((date(2026, 1, 1) + timedelta(days=1)).isoformat())

Answer: 2026-01-02

Why: Adding one day moves the date to January 2.

12 · Predict output

What appears on the screen?

from datetime import date
print(date(2026, 12, 31).strftime("%Y/%m/%d"))

Answer: 2026/12/31

Why: The numeric pattern produces year, month, and day separated by slashes.

Try the questions first

Open the practice page for Python Dates and Times with datetime and answer the same 12 questions with instant feedback.

Practice this lesson