Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Working with dates and times

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

40 minutes

The receipt with the wrong date

A shop receipt says 05/12/2026. Is that 5 December or 12 May? In the UK it is 5 December. In the USA it is 12 May.

Programs need to read dates and show dates. Python has pre-written code for this. This lesson shows three commands.

Key words

WordWhat it meansExample
datetimeA Python library (a set of ready-made code) for dates and times.from datetime import datetime
now()Gives the date and time right now.datetime.now()
Format codeA short code, such as %d, that stands for one part of a date.%Y is the year
strftime()Turns a date into text. The name means "string from time".when.strftime("%d/%m/%Y")
strptime()Turns text into a date. The name means "string parse time".datetime.strptime("25/12/2026", "%d/%m/%Y")
StringText inside quote marks."25/12/2026"
FunctionA named piece of code that does one job.len()
ValidationChecking that data is correct and makes sense before you use it.Is this a real date?

The cells students fill in are left blank in this preview.

Try it: the date right now

Code: python
from datetime import datetime

now = datetime.now()
print(now)
print(now.strftime("%d/%m/%Y"))
print(now.strftime("%H:%M"))

# Run it twice. The answer changes, because the time moves on.

A date is not the same as text

Inside Python, a date is a special kind of value. It is not the same as the letters and digits you see on a screen.

Text is only letters and digits. To show a date to a person, you make text from it. To use text that a person typed, you make a date from it.

Two commands do these jobs. They go in opposite directions.

Worked example: the format codes

CodeStands forExample for 25 December 2026, 9:30 am
%dDay of the month, two digits25
%mMonth as a number, two digits12
%YYear, four digits2026
%BMonth name in fullDecember
%ADay of the week in fullFriday
%HHour on a 24 hour clock09
%MMinutes30

The cells students fill in are left blank in this preview.

Try it: date to text

Code: python
from datetime import datetime

when = datetime(2026, 12, 25, 9, 30)

print(when.strftime("%d/%m/%Y"))
print(when.strftime("%A %d %B %Y"))
print(when.strftime("%H:%M"))

# Change the format text. Try "%Y-%m-%d".
# Anything that is not a code, such as the / sign, is copied as it is.

Quick check: format codes

Have a go

Quick check: format codes

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Text to date: strptime

strptime() reads text and makes a date. You must tell it how the text is laid out, with the same format codes.

If the text does not match, Python stops with an error called ValueError. That is useful. It is a free validation check: a real date passes, and "31/02/2026" or "tomorrow" fails.

The lesson goes on

8 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.