CALENDAR ARITHMETIC

Checking why reversing a month shift may not restore the date

Adding one month to January 31 gives February 29 in 2024. Subtracting one month from that result gives January 29, so the reverse shift does not restore the original date.

How the month shift works

The add-months tool shifts the year and month by an integer number of months and keeps the day number when the destination month has it. When the destination month is shorter, the day is clamped to the last day of that month. January 31 plus one month would be February 31, which does not exist, so the tool returns the last day of February. In 2024 that is February 29. The rule is simple and predictable, and it has one consequence that surprises people: clamping discards information. The result no longer records that the original day was 31.

Reversing the shift

Apply a shift of minus 1 month to 2024-02-29. The month moves back to January, and the day number is 29, which January has, so no clamping occurs and the result is 2024-01-29. The reverse operation receives the 29th as its source day. It cannot know that the date came from the 31st. Forward and reverse shifts are therefore not exact inverses once clamping has happened. The pair 2024-01-31 to 2024-02-29 to 2024-01-29 loses two days on the round trip, and the loss comes from the clamping rule. Counting the days between the start and the end of the round trip makes the point: 2024-01-31 and 2024-01-29 are two days apart even though the net shift was zero months, which shows the loss in plain calendar terms.

Several sources, one intermediate date

Three different January dates all land on the same February date in 2024. January 29 plus one month is February 29. January 30 plus one month clamps to February 29. January 31 plus one month clamps to February 29 as well. Going back from February 29 can only choose one of them, and the tool chooses January 29 because the day number is kept whenever the destination month has it. Many source days map to one destination, so the mapping cannot be reversed without extra information. This many-to-one pattern is the mathematical reason for the loss, and it is the same reason a function that maps three inputs to one output has no inverse.

The other order: subtract first, then add

The loss also appears when the order is swapped. Start at 2024-03-31 and subtract one month. February 2024 ends on the 29th, so the result is 2024-02-29. Adding one month to that returns 2024-03-29, not the original March 31. The same lesson holds in both directions: whichever step lands in a shorter month discards the day number, and the following step cannot recover it. A check that moves a date away and back is a quick test of any date library, and a date that comes back unchanged for every day from the 28th to the 31st is rare. Try those four days in each month before relying on a round trip.

A non-leap example

Take 2023-01-31. Adding one month gives 2023-02-28, since February 2023 ends on the 28th. Subtracting one month from that gives 2023-01-28. The round trip loses three days this time, against two in the leap year. Month ends in the other direction also clamp. From 2023-03-31, adding one month gives 2023-04-30 because April has 30 days, and subtracting one month from that gives 2023-03-30. The pattern appears wherever a 31-day month is followed by a shorter one, and the amount lost equals the gap between the two month lengths.

Year-sized shifts and the leap day

Shifts of 12 months show the same behavior around February 29. Adding 12 months to 2024-02-29 gives 2025-02-28, because 2025 has no leap day. Subtracting 12 months from that gives 2024-02-28, not the original leap day. Anyone whose anniversary is February 29 therefore sees an observed date of February 28 in common years, and a repeated back-and-forth shift drifts to the 28th. Some policies prefer March 1 for leap-day anniversaries, and the tool does not choose between those policies. It applies the clamp. Over a four-year cycle the leap-day anniversary therefore appears on February 29 once and on February 28 three times, and a record that stores only the observed date cannot tell the person's true anniversary from the shifted one.

Keep the original as an anchor

If a workflow needs to return to the starting date, store the starting date. Compute each later date from that anchor with the full shift, for example always adding n months to 2024-01-31 instead of adding one month to the previous result. That keeps the day number 31 available whenever the destination month has it. In a spreadsheet, the anchor sits in one cell and every later row adds its own month count to that cell, so editing the anchor updates the whole series at once. The next page in this set shows how repeated one-month steps drift away from direct shifts. The anchor approach also makes results reproducible, because any single date in the series can be recomputed without the chain before it.

Choosing a policy for month-end dates

Several policies are in use. One clamps to the last day of the shorter month, as this tool does. Another rolls overflow into the next month, so January 31 plus one month becomes March 2 or March 3. A third treats month-end dates specially and always returns the last day of the destination month, so a series that begins on the last day of a month stays on the last day. Which policy applies depends on the agreement, the software or the regulation behind the schedule. Check it before you reuse a date from this tool.

What the tool does and does not store

The tool performs the stated shift on the entered date and returns the result. It stores no hidden recurrence preference and remembers nothing between calls, so it cannot restore a date from a previous call. It also applies no business-day adjustment, no time zone and no clock time. A date that falls on a weekend stays on the weekend, and a result on a closure day is returned as is. Any rule that moves a date to the next working day has to be applied as a separate step. The worked cases are arithmetic on calendar dates. For payments, leases, subscriptions or legal periods, use the date rule in the governing document, and treat this tool as a way to check the arithmetic under the clamping rule.

Reproduce the checks

Clamped forward shift

Open the Add Months Calculator and enter:

Calendar date
2024-01-31
Months
1

Expected output:

Date

2024-02-29

The original day cannot exist in this destination month.

Reverse the intermediate date

Open the Add Months Calculator and enter:

Calendar date
2024-02-29
Months
-1

Expected output:

Date

2024-01-29

The reverse operation uses the intermediate twenty-ninth.

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