Python Date Examples: Yesterday, Tomorrow, Weekends and Leap Years

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.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

from 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.

Display the next five days

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

Check leap years correctly

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

Find weekends within a date range

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)

Find weekday from user input

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

First Monday of a month

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

First day of next month

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

Find the last day of the current month

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

Add hours and minutes

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

Validate a date string

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

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

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.

Frequently asked questions

What does date.today() return?

The current local date on the machine running Python.

Is a year divisible by 4 always leap?

No. Century years must also be divisible by 400.

Why use timedelta?

It handles day and month rollover in date arithmetic.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer