Date & Time
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — The datetime Module
Python has a built-in module called `datetime` that lets you work with dates and times. To use it, you need to import it first.
from datetime import datetime
now = datetime.now()
print(now)
print(type(now))`datetime.now()` gives you the current date and time. The result is a special `datetime` object, not a regular string.
Exercise 2 — Creating a Specific Date
You can create a specific date using `datetime(year, month, day)`. This is useful when you want to store a known date.
from datetime import datetime
catch_date = datetime(2024, 3, 15)
print(catch_date)The order is always: year, month, day. You can also add hours, minutes, seconds: `datetime(2024, 3, 15, 14, 30, 0)`.
Exercise 3 — Accessing Date Parts
A `datetime` object has attributes that let you get each part separately.
from datetime import datetime
d = datetime(2024, 7, 25, 14, 30)
print(d.year)
print(d.month)
print(d.day)
print(d.hour)
print(d.minute)Available Attributes
`.year`, `.month`, `.day`, `.hour`, `.minute`, `.second`. Each returns an integer.
Exercise 4 — datetime Attributes
from datetime import datetime
d = datetime(2024, 7, 25)
print(d.month)What is printed?
2572024JulyHint : `.month` returns the month as a number, not a name.
Exercise 5 — Create a Date
Complete the code to create the date January 1st, 2025.
Hint : `datetime()` takes year first, then month, then day.
Exercise 6 — Get the Day
Complete the code to print only the day of the date.
Hint : Which attribute gives you just the day number?
Exercise 7 — Formatting Dates with strftime
`strftime()` lets you format a date as a readable string. You use format codes to control the output.
from datetime import datetime
d = datetime(2024, 3, 15)
print(d.strftime("%d/%m/%Y"))
print(d.strftime("%B %d, %Y"))Common Format Codes
- `%d` = day (01-31) - `%m` = month number (01-12) - `%B` = full month name - `%Y` = 4-digit year - `%H` = hour (00-23) - `%M` = minute (00-59)
Exercise 8 — strftime Output
from datetime import datetime
d = datetime(2024, 3, 15)
print(d.strftime("%Y-%m-%d"))What is printed?
15/03/20242024-03-15March 15, 20242024-3-15Hint : `%Y` gives the 4-digit year, `%m` the 2-digit month, `%d` the 2-digit day.
Exercise 9 — Format Codes
Match each `strftime` format code with what it returns:
%d%m%Y%B%HExercise 10 — Format a Date
Complete the code to display the date as "15/03/2024".
Hint : The format is day/month/year. Which codes give 2-digit day, 2-digit month, and 4-digit year?
Exercise 11 — Comparing Dates
You can compare `datetime` objects using `<`, `>`, `==`. This is useful to find which date is more recent.
from datetime import datetime
d1 = datetime(2024, 1, 1)
d2 = datetime(2024, 12, 25)
print(d1 < d2)
print(d1 > d2)
print(d1 == d2)A more recent date is "greater than" an older one. `d2` (December) is greater than `d1` (January).
Exercise 12 — Codémon Catch Tracker
Create a Codémon catch date tracker! 1. Create 3 catch dates: - Aquachu: March 15, 2024 - Pyrochu: July 4, 2024 - Florachu: December 25, 2024 2. For each, print: `"Aquachu caught on 15/03/2024"` 3. Find and print which Codémon was caught most recently (compare dates with `>`)
from datetime import datetime
# 1. Complete the list with Pyrochu (July 4, 2024) and Florachu (December 25, 2024)
catches = [
("Aquachu", datetime(2024, 3, 15)),
]
# 2. Print formatted dates
# 3. Find the most recent catch