Python’s standard datetime module supports date arithmetic, calendar calculations and date formatting. This page brings together practical examples from the original Plus2Net date-sample tutorial.
Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.
View on GitHubfrom datetime import date, timedelta
today = date.today()
print("Today:", today)
print("Yesterday:", today - timedelta(days=1))
print("Tomorrow:", today + timedelta(days=1))
Output varies with the date on your computer.
Output:
Today: 2026-11-13
Yesterday: 2026-11-12
Tomorrow: 2026-11-14
The outputs change with the execution date. The source’s 2021 examples were snapshots, not fixed expected results.
Use timedelta(days=i) for consecutive dates. A fixed starting date makes this example reproducible.
from datetime import date, timedelta
start = date(2026, 1, 29)
for i in range(5):
print(start + timedelta(days=i))
Output:
2026-01-29
2026-01-30
2026-01-31
2026-02-01
2026-02-02
The original version checked only divisibility by 4, which misclassifies century years such as 1900. The full rule handles 100 and 400.
import calendar
for year in (1900, 2000, 2024, 2025):
print(year, calendar.isleap(year))
Output:
1900 False
2000 True
2024 True
2025 False
The original page accepts day/month/year text and finds Saturdays and Sundays. Use a fixed range first.
from datetime import datetime, timedelta
start = datetime.strptime("1 Dec 2021", "%d %b %Y").date()
end = datetime.strptime("12 Dec 2021", "%d %b %Y").date()
days = (end - start).days + 1
for offset in range(days):
current = start + timedelta(days=offset)
if current.weekday() >= 5:
print(current.strftime("%Y-%m-%d (%A)"))
Output:
2021-12-04 (Saturday)
2021-12-05 (Sunday)
2021-12-11 (Saturday)
2021-12-12 (Sunday)
Parse the format explicitly so 31/12/2022 has a predictable interpretation.
from datetime import datetime
text = input("Enter date dd/mm/YYYY: ")
value = datetime.strptime(text, "%d/%m/%Y")
print(text, value.strftime("%A"))
Output:
31/12/2022 Saturday
The first Monday falls at day 1 + (0 - first_day.weekday()) % 7. This corrects the source’s special-case arithmetic.
from datetime import date
year, month = 2026, 11
first_day = date(year, month, 1)
first_monday = first_day.replace(day=1 + (-first_day.weekday()) % 7)
print(first_monday)
Output:
2026-11-02
Handle December-to-January rollover without using hardcoded 12-month assumptions elsewhere.
from datetime import date
d = date(2019, 12, 15)
first_next = date(d.year + (d.month == 12), d.month % 12 + 1, 1)
print(first_next)
Output:
2020-01-01
Subtract one day from the first date of the next month.
from datetime import date, timedelta
d = date(2024, 2, 10)
next_month = date(d.year + (d.month == 12), d.month % 12 + 1, 1)
print(next_month - timedelta(days=1))
Output:
2024-02-29
datetime includes time. With a fixed timestamp, the result is reproducible.
from datetime import datetime, timedelta
start = datetime(2026, 5, 1, 11, 25)
print(start + timedelta(hours=2, minutes=35))
Output:
2026-05-01 14:00:00
Invalid dates raise ValueError; handle this for user input.
from datetime import datetime
try:
value = datetime.strptime("31/02/2025", "%d/%m/%Y")
print(value.date())
except ValueError:
print("Invalid date")
Output:
Invalid date
input() returns a string. Convert and validate the value before doing arithmetic.Calculate the previous and next day for 1 January, then check February 1900 and February 2000. Use the companion practice notebook for runnable examples and self-checks.
The current local date on the machine running Python.
No. Century years must also be divisible by 400.
It handles day and month rollover in date arithmetic.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.