Dates and Times
datetime gives you date, time, datetime and timedelta. strftime turns a date into text using codes such as %Y-%m-%d, and strptime reads text into a date. Subtracting two dates gives a timedelta.
Example
from datetime import datetime, date, timedelta
d = date(2026, 3, 15)
print(d.year, d.month, d.day)
print(d.strftime("%d %B %Y"))
print(d + timedelta(days=30))
start = datetime(2026, 1, 1)
end = datetime(2026, 1, 31)
print((end - start).days)
print(datetime.strptime("2026-05-20", "%Y-%m-%d").weekday())Result
2026 3 15 15 March 2026 2026-04-14 30 2
Exercise
Add 7 days to a date.
d + (days=7)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.