Python Arena
Step 3 of 3

Formatting and reading dates

A printed datetime looks like 2026-03-14 09:05:00, which isn't how people usually write dates. The method strftime (string format time) turns a datetime into text in any format you like. You describe the format with codes that start with %:

from datetime import datetime

moment = datetime(2026, 3, 14, 9, 5)
print(moment.strftime("%d.%m.%Y"))
print(moment.strftime("%m/%d/%Y %H:%M"))
print(moment.strftime("%A %d %B %Y"))
Output
14.03.2026
03/14/2026 09:05
Saturday 14 March 2026

Everything in the format that isn't a code (the dots, slashes, spaces and any words) is copied into the result as it is. The most useful codes:

Code Means For the moment above
%d day of the month, two digits 14
%m month, two digits 03
%Y year, four digits 2026
%H hour, 00 to 23 09
%M minutes 05
%S seconds 00
%A name of the weekday Saturday
%B name of the month March

The full list is in the documentation of the datetime module.

Small m, capital M

The classic mistake: %m is the month, %M the minutes. Mix them up and there's no error, just a wrong date:

from datetime import datetime

moment = datetime(2026, 3, 14, 9, 5)
print(moment.strftime("%d.%M.%Y"))
Output
14.05.2026

Python can't know you meant the month, so check the output of date formatting carefully.

From text to a datetime

strptime (string parse time) works the other way round: it reads a date written as text in a format you describe, and makes a datetime of it. You call it on the type itself, datetime.strptime(text, format):

from datetime import datetime

text = input("Date (dd.mm.yyyy): ")
day = datetime.strptime(text, "%d.%m.%Y")
print(day)
print(day.strftime("%A"))
Input
24.12.2026
Output
Date (dd.mm.yyyy): 24.12.2026
2026-12-24 00:00:00
Thursday

When reading, a single-digit day or month is fine too: 5.1.2026 reads as 5 January. But if the text doesn't match the format, or the date doesn't exist, strptime raises a ValueError, such as ValueError: time data '2026-12-24' does not match format '%d.%m.%Y'. Catch it with try and except ValueError, just like a failed int(...).

Your task

Challenge 1 of 3

Fill in the two format strings so the program prints the moment as day/month/year on the first line and as hours and minutes on the second:

04/07/2026
18:30

Challenge 2

Write a function weekday_of(text) that gets a date as text in the form year-month-day, such as "2026-10-15", and returns the name of its weekday, such as "Thursday". Read the text with strptime and get the name with strftime.

Challenge 3

A plant needs water every three days. Write a program that asks for the first watering day (as day.month.year) and the number of waterings, and prints each watering day with its weekday, in the format of the example:

First watering (dd.mm.yyyy): 27.10.2026
How many times: 4
Tuesday 27.10.2026
Friday 30.10.2026
Monday 02.11.2026
Thursday 05.11.2026