Date and time values with the datetime module

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Date and time values with the datetime module

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly imports the datetime tool so you can use datetime.now()?
A from datetime import datetime
B import datetime from datetime
2. After running from datetime import datetime, which line gets the current date and time?
A now = datetime.today_time
B now = datetime.now()
3. Which code formats a datetime value called now as a day/month/year text?
A now.strftime("%d/%m/%Y")
B now.format("%d/%m/%Y")
4. In strftime, what does the code %Y give you?
A The current minute, such as 45
B The four-digit year, such as 2025
5. Which strftime format code would show the hour in 24-hour time, like 14:30?
A "%H:%M"
B "%D:%S"

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1now = datetime.now() AThe brackets are missing, so the function is never called and the current time is not stored. It should be datetime.now().
2now.strftime("%d/%m/%Y") BGets the current date and time and stores it in the variable now
3Import the datetime class from the datetime module so you can use it in your program Cfrom datetime import datetime
4Subtract an earlier date from a later date to find the duration between them DFormats the date as text in day/month/year form, such as 25/03/2026
5now = datetime.now Egap = end - start
6now.strftime(%H:%M) FThe format code is not in quotes. It should be now.strftime("%H:%M") because strftime needs a string.

Definitions

from datetime import datetime

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
from datetime ______ datetime
2.
now = datetime.___()
3.
print(now.________("%d/%m/%Y"))
4.
start = datetime.now() duration = datetime.now() _ start
5.
print(now.strftime("%_"))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A from datetime import datetime
B now = datetime.now
C print(now.year)
Question 2
A from datetime import datetime
B now = Datetime.now()
C d = now.strftime("%d/%m/%Y")
D print(d)
Question 3
A from datetime import datetime
B start = datetime(2025, 3, 1)
C end = datetime(2025, 15, 3)
D print(end - start)
Question 4
A from datetime include datetime
B now = datetime.now()
C print(now.strftime("%Y"))
Question 5
A from datetime import datetime
B now = datetime.datetime.now()
C print(now.strftime("%H:%M"))

Answer Key

Date and time values with the datetime module
Section A: Quick Questions
Q1
A — from datetime import datetime
Q2
B — now = datetime.now()
Q3
A — now.strftime("%d/%m/%Y")
Q4
B — The four-digit year, such as 2025
Q5
A — "%H:%M"
Section B: Match the Pairs
Q1
now = datetime.now() → Gets the current date and time and stores it in the variable now
Q2
now.strftime("%d/%m/%Y") → Formats the date as text in day/month/year form, such as 25/03/2026
Q3
Import the datetime class from the datetime module so you can use it in your program → from datetime import datetime
Q4
Subtract an earlier date from a later date to find the duration between them → gap = end - start
Q5
now = datetime.now → The brackets are missing, so the function is never called and the current time is not stored. It should be datetime.now().
Q6
now.strftime(%H:%M) → The format code is not in quotes. It should be now.strftime("%H:%M") because strftime needs a string.
Section C: Definitions
Q1
from datetime import datetime — Brings in the datetime tool so Python can get the current date and time with datetime.now(), format it with strftime, and subtract two dates to find a duration.
Section D: Fill in the Blanks
Q1
from datetime import datetime
Q2
now = datetime.now()
Q3
print(now.strftime("%d/%m/%Y"))
Q4
start = datetime.now() duration = datetime.now() - start
Q5
print(now.strftime("%Y"))
Section E: Spot the Error
Q1
B — datetime.now is a function, so it needs brackets to run. It should be now = datetime.now().
Q2
B — Python is case-sensitive. The imported name is datetime with a lowercase d, so it should be now = datetime.now().
Q3
C — datetime() takes year, month, day in that order. Month 15 does not exist, so it should be datetime(2025, 3, 15).
Q4
A — Python uses the import keyword, not include. The line should be from datetime import datetime.
Q5
B — After from datetime import datetime, the class is called datetime directly. Writing datetime.datetime fails. It should be now = datetime.now().