2026, Jan 02 23:00

Avoid strftime pitfalls: compute the next Sunday in Python using timedelta or date.weekday(), then format as dd.mm.yyyy

Learn how to compute and format the next Sunday in Python: use timedelta or date.weekday() for the date, then strftime to print dd.mm.yyyy without errors.

Formatting the next Sunday in Python without tripping over strftime

Finding the next Sunday and formatting it as a human-friendly string is a common datetime task. The arithmetic is straightforward with timedelta, and strftime handles presentation. The subtle pitfall is trying to combine both but forgetting that strftime must be called on a date object, not the other way around.

Reproducing the situation

The logic below computes the next Sunday as a date object, and formats today’s date separately. The first line prints a raw ISO date, the second line prints a formatted string.

import datetime as dt
import time as tm

print(dt.date.today() + dt.timedelta(7 - int(tm.strftime("%u"))))
print(dt.date.today().strftime("%d.%m.%Y"))

The output will look like this:

2025-05-04
27.04.2025

What you actually want is the next Sunday formatted as dd.mm.yyyy.

What’s really going on

strftime is for producing a string representation for humans. It won’t change how a date or datetime prints unless you explicitly call it on the computed date. In other words, you can do arithmetic first to get the correct date, and then call strftime on that result to format it.

The fix, and a cleaner option

If you want to keep the original approach, just apply strftime to the computed next Sunday. The missing piece is the final method call in the chain.

import datetime as dt
import time as tm

pretty_sun = (
    dt.date.today() + dt.timedelta(7 - int(tm.strftime("%u")))
).strftime("%d.%m.%Y")
print(pretty_sun)

An alternative that reads better is to rely on date.weekday(). According to the docs, weekday() returns 0 for Monday through 6 for Sunday. That makes the offset trivial: add 6 - weekday() days to get Sunday of the current week (or today if today is Sunday).

from datetime import date as d, timedelta as td

anchor_day = d.today()
next_sun = anchor_day + td(days=6 - anchor_day.weekday())
print(next_sun.strftime("%d.%m.%Y"))

Reference: date.weekday() returns an integer where Monday is 0 and Sunday is 6.

Why this detail matters

Mixing computation and presentation often leads to hard-to-spot bugs or unreadable code. Keeping the arithmetic confined to timedelta and weekday() makes intent obvious, and using strftime only for formatting avoids accidental reliance on string representations during computation. Small improvements in clarity pay off when you revisit the code or hand it to someone else.

Takeaways

Compute the target date first, then format it. If you’re aiming for next Sunday, weekday()-based arithmetic is concise and explicit, and strftime turns the result into the exact string you need. When in doubt, favor a couple of well-named variables over a dense one-liner—your future self will thank you.