Date Math Traps: Month Ends, Leap Years, Weeks
Date arithmetic looks easy until a month is too short, a year has a leap day, or a week belongs to the wrong year. Most surprises come from five rules, and each one has a definite answer.
Two different questions: a gap and a shift
A gap counts how many days lie between two dates. A shift moves one date by an amount of time and asks where you land. A gap of 30 days is always 30 days, but a shift of one month is 28, 29, 30 or 31 days depending on where you start. That is why 2026-01-31 plus one month and 2026-01-31 plus 30 days are different dates.
A month that is too short ends on its last day
January has a 31st and February does not, so adding one month to 2026-01-31 gives the last day of the target month: 2026-02-28. In a leap year the same sum gives 2024-02-29. Months are added before days, so 2026-01-31 plus one month and one day is 2026-03-01: first 2026-02-28, then one more day. Adding 30 days instead counts real days and gives 2026-03-02, with no adjusting.
The start day is not counted
The difference between two dates is the later one minus the earlier one. 2026-10-07 to 2026-10-08 is 1 day. If you want to count both ends, for example the days of a trip from the 1st to the 5th, the gap is 4 days and the count with both ends is 5, so add 1.
Leap days and anniversaries
2024-02-29 plus one year is 2025-02-28, the last day of February, because 2025 has no 29th. The span from 2024-02-29 to 2025-02-28 is 365 days, and it reads as exactly 1 year 0 months 0 days, matching the anniversary. A breakdown into years, months and days is worked out by moving the earlier date forward by whole months and counting the days that remain, so it agrees with how months are added.
Weekdays are not working days
Counting Monday to Friday between two dates is easy: from 2026-01-05 to 2026-01-12 there are 7 days and 5 weekdays, with the end date not counted. From 2026-01-01 to 2026-12-25 there are 358 days, which is 51 weeks and 1 day, and 256 weekdays. Public holidays differ by country and year, so a weekday count is only a rough working-day count. Take the holidays off yourself.
ISO weeks can belong to the other year
ISO 8601 weeks start on Monday, and week 1 is the week with the year’s first Thursday. So early January can fall in week 52 or 53 of the previous year, and late December in week 1 of the next. 2021-01-03 is in week 53 of 2020, 2024-12-30 is in week 1 of 2025, and 2026-01-01 is in week 1. If a report says “week 1” for a date in late December, this is why.
A checklist before you trust a result
- Decide whether you want a gap or a shift.
- Decide whether both end dates count.
- For month-end dates, check the clamped result.
- For work days, remove the holidays yourself.
- For weeks, say which numbering you use.
Time zones and daylight saving do not change calendar-day arithmetic, because a date without a time of day has no clock to shift.