← VCE Software Development U4O1

Date and time values with the datetime module

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.

Understanding Date and time values with the datetime module in VCE Software Development U4O1

What it does: This line must come before datetime.now() so Python knows where the datetime tool comes from. Subtracting one datetime from another gives a duration (a timedelta), which shows how much time passed. The correct pattern is from module import name. So it is from datetime import datetime. datetime.now() is a function call, so it needs brackets. It returns the current date and time. The method that turns a datetime into formatted text is strftime(). A datetime has no format() method like this. %Y is the four-digit year. Minutes use a lowercase %M, and a lowercase %y gives a two-digit year. %H is the hour (24-hour clock) and %M is the minute. So "%H:%M" gives a time like 14:30.

Common mistakes: datetime.now is a function, so it needs brackets to run. It should be now = datetime.now(). Python is case-sensitive. The imported name is datetime with a lowercase d, so it should be now = datetime.now().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 115–124) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly imports the datetime tool so you can use datetime.now()?
import datetime from datetimefrom datetime import datetime
Matches
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().
Applying
Meanings
Term
from datetime import datetime
Meaning
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.
Practice
from datetime import datetime
Testing
Gaps
from datetime datetime
Check Answer
Spots
1from datetime import datetime
2now = datetime.now

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Lion' badge — Inheritance King, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly imports the datetime tool so you can use datetime.now()?
A. from datetime import datetime ✓
B. import datetime from datetime
The correct pattern is from module import name. So it is from datetime import 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() ✓
datetime.now() is a function call, so it needs brackets. It returns the current date and time.
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")
The method that turns a datetime into formatted text is strftime(). A datetime has no format() method like this.
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 ✓
%Y is the four-digit year. Minutes use a lowercase %M, and a lowercase %y gives a two-digit year.
5. Which strftime format code would show the hour in 24-hour time, like 14:30?
A. "%H:%M" ✓
B. "%D:%S"
%H is the hour (24-hour clock) and %M is the minute. So "%H:%M" gives a time like 14:30.

Match the Pairs

now = datetime.now() → Gets the current date and time and stores it in the variable now
now.strftime("%d/%m/%Y") → Formats the date as text in day/month/year form, such as 25/03/2026
Import the datetime class from the datetime module so you can use it in your program → from datetime import datetime
Subtract an earlier date from a later date to find the duration between them → gap = end - start
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().
now.strftime(%H:%M) → The format code is not in quotes. It should be now.strftime("%H:%M") because strftime needs a string.

Key Term

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.

Fill in the Blanks

1. from datetime import datetime
Hint: Type the keyword that brings the datetime class into your program.
2. now = datetime.now()
Hint: Type the method name that gets the current date and time.
3. print(now.strftime("%d/%m/%Y"))
Hint: Type the method name that formats a date as text using format codes.
4. start = datetime.now() duration = datetime.now() - start
Hint: Type the operator used to subtract one date from another to find a duration.
5. print(now.strftime("%Y"))
Hint: Type the capital letter format code that gives the four-digit year.

Spot the Error

Question 1
A from datetime import datetime
B now = datetime.now
C print(now.year)
Line B — datetime.now is a function, so it needs brackets to run. It should be now = datetime.now().
Question 2
A from datetime import datetime
B now = Datetime.now()
C d = now.strftime("%d/%m/%Y")
D print(d)
Line B — Python is case-sensitive. The imported name is datetime with a lowercase d, so it should be now = datetime.now().
Question 3
A from datetime import datetime
B start = datetime(2025, 3, 1)
C end = datetime(2025, 15, 3)
D print(end - start)
Line C — datetime() takes year, month, day in that order. Month 15 does not exist, so it should be datetime(2025, 3, 15).
Question 4
A from datetime include datetime
B now = datetime.now()
C print(now.strftime("%Y"))
Line A — Python uses the import keyword, not include. The line should be from datetime import datetime.
Question 5
A from datetime import datetime
B now = datetime.datetime.now()
C print(now.strftime("%H:%M"))
Line B — After from datetime import datetime, the class is called datetime directly. Writing datetime.datetime fails. It should be now = datetime.now().

Student with a class code? Join your class →