CALENDAR ARITHMETIC

Round-tripping a calendar date through an ISO week date

An ISO week date needs three fields to name one calendar day: the week-year, the week number and the weekday. Without the weekday, only the week can be recovered.

What an ISO week date contains

ISO 8601 defines a week date as a week-year, a week number from 1 to 53 and a weekday from 1 to 7, with Monday as 1 and Sunday as 7. Week 1 is the week that contains January 4, which is the same as saying it is the week with the year's first Thursday. Because weeks always run Monday to Sunday, the first days of January can belong to the last week of the previous year, and the last days of December can belong to week 1 of the next. This is why the week-year can differ from the ordinary calendar year near New Year.

Converting 2021-01-01

The week-number tool takes the date 2021-01-01, a Friday, and returns week-year 2020, week 53 and weekday 5. The ISO week that contains that Friday began on Monday 2020-12-28, and 2020 has 53 weeks, so the Friday belongs to the last week of 2020. All three outputs are needed. Writing 2021-W53-5 would give a week-year that does not match the week number, and 2020-W53 with no weekday names a whole week instead of one day. The result is the triple (2020, 53, 5), and the original calendar year of 2021 does not appear in it.

Reversing the conversion

The reverse tool locates Monday of week 1 of the week-year, using January 4 as the anchor, then adds 7 × (week minus 1) + (weekday minus 1) days. For week-year 2020, week 53 and weekday 5, Monday of week 1 is 2019-12-30, and adding 7 × 52 + 4 = 368 days gives 2021-01-01. The round trip returns the original date. This is a deterministic check that the forward and reverse tools are consistent, and it also shows why the three fields are all required: remove any one and the formula cannot run.

Changing only the weekday

Keep week-year 2020 and week 53 but set the weekday to 1. The tool returns 2020-12-28, the Monday of that week. Weekday 7 returns Sunday 2021-01-03, and weekday 5 returns Friday 2021-01-01. All seven dates belong to the same ISO week even though they fall in two different calendar years: 2020-12-28 to 2020-12-31 in 2020, and 2021-01-01 to 2021-01-03 in 2021. A week label alone therefore does not identify a date, and a system that stores only the week-year and week number loses the day.

Another boundary: December 31, 2018

The date 2018-12-31 is a Monday. Its ISO week contains January 3, 2019, and the week with the first Thursday of 2019 is week 1 of that year, so the date converts to week-year 2019, week 1 and weekday 1. The reverse tool takes (2019, 1, 1) and returns 2018-12-31. This is the opposite direction of the previous case: a December date placed in the next week-year. A date in early January can belong to the previous week-year, and a date in late December can belong to the next, so neither boundary can be handled by appending the week number to the visible calendar year. The Thursday rule explains both cases: the week belongs to whichever year holds its Thursday. The Thursday of the week of 2018-12-31 is 2019-01-03, and the Thursday of the week of 2021-01-01 is 2020-12-31.

A date in an ordinary week

Away from the boundary the three fields look simple. The date 2021-01-04 is a Monday, and the week-year is 2021 with week 1 and weekday 1, since week 1 of 2021 contains January 4. The conversion is straightforward, the week-year matches the calendar year, and the round trip still needs all three fields. Moving to Sunday 2021-01-10 gives week 1 with weekday 7, and Monday 2021-01-11 starts week 2, so the week number changes exactly at the Monday boundary and never in the middle of a week. Running a few ordinary dates alongside the boundary dates is a useful test: if code works for the boundary cases but fails on January 4, or the reverse, the anchor rule has been misread.

Where week dates are used

Week dates appear in manufacturing and supply-chain schedules, in European business planning, in some public-transit and school calendars and in data formats that follow ISO 8601. A production plan might refer to week 14, and a shipping record might use a week-year and week number to avoid month boundaries. These systems benefit from a fixed seven-day period. Their risk is the year boundary, where a label like 2021-W53 or week 1 of 2019 is correct but looks wrong to a reader who expects the calendar year. Always check the first and last week of a year in any imported data. A useful test file contains 2018-12-31, 2021-01-01 and 2021-01-04, because together they exercise a December date in the next week-year, a January date in the previous one and an ordinary start of week 1.

Writing week dates and avoiding the wrong year code

The extended ISO 8601 text form is the week-year, a hyphen, the letter W, the two-digit week, a hyphen and the weekday, so 2021-01-01 is written 2020-W53-5. The compact form drops the hyphens: 2020W535. A frequent bug comes from formatting a date with the calendar-year code and the week code together. In C and Python strftime, %Y is the calendar year, %G is the ISO week-year and %V the ISO week number, so %G-W%V-%u gives the correct triple, while %Y-W%V-%u prints 2021-W53-5 for January 1, 2021, a week label that does not exist. The bug stays hidden for most of the year and appears only in the last and first days of December and January.

What the round trip proves

A successful round trip shows that two tools apply the same ISO week rule to a date and back. It does not independently validate every calendar rule, and it says nothing about regional week definitions. Some countries number weeks from Sunday or count the week containing January 1 as week 1, and those schemes give different week numbers for the same dates. The ISO convention is Monday-based with week 1 containing January 4, and that is what the tools implement. State the convention beside any week number you share. For example, January 1, 2021 is in week 53 of 2020 under ISO rules, but in a scheme that starts the year's first week on January 1 it would be week 1 of 2021. Both labels are valid in their own system, and they cannot be mixed in one table without a column that names the scheme.

Reproduce the checks

Forward conversion

Open the Week Number Calculator and enter:

Calendar date
2021-01-01

Expected output:

Week year

2020

Week

53

Weekday

5

Keep all three outputs.

Complete reverse conversion

Open the Iso Week To Date Calculator and enter:

Week year
2020
Week
53
Weekday
5

Expected output:

Date

2021-01-01

The original calendar date is restored.

Change only the weekday

Open the Iso Week To Date Calculator and enter:

Week year
2020
Week
53
Weekday
1

Expected output:

Date

2020-12-28

This is another date within the same week.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks