Batu Lab NotesPractical developer guides

Convert a year and week back to its Monday

By Batu ยท English technical notes

Also published in our Blogger archive.

When a report stores an ISO year and ISO week, reconstruct its Monday with ISO calendar fields rather than counting forward from January 1. January 1 can belong to the previous ISO year, and some ISO years have week 53. date.fromisocalendar(year, week, day) expresses the conversion directly; ISO weekday 1 means Monday.

The example converts ISO year 2025, week 1 to 2024-12-30. That date looks surprising only if the ISO year is confused with the Gregorian year: it is the Monday that starts ISO week 1 of 2025. A second assertion round-trips the result through isocalendar() and confirms the selected year, week, and weekday. The assertion demonstrates the standard-library conversion for this valid input; it does not validate user-entered week strings or prescribe a fiscal-week scheme.

fromisocalendar() validates its arguments and raises ValueError for an invalid ISO week date, such as week 53 in a year that has only 52 ISO weeks. Let that error propagate when invalid values are a programming error, or catch it at an input boundary to return an application-specific validation message. This constructor was added in Python 3.8, so Python 3.8 is the minimum version for this exact approach. The official date.fromisocalendar() documentation describes it as the inverse of isocalendar().

AI assistance disclosure: This article was drafted with AI assistance and should be reviewed in the context of its application.

from datetime import date


def monday_for_iso_week(year: int, week: int) -> date:
    return date.fromisocalendar(year, week, 1)


monday = monday_for_iso_week(2025, 1)
assert monday == date(2024, 12, 30)
assert monday.isocalendar() == (2025, 1, 1)

print(monday.isoformat())
2024-12-30

Sources