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().
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
from datetime import datetimeFill in the Blanks
Spot the Error
Student with a class code? Join your class →