DateBuffet

Date Math Without Mistakes: Adding and Subtracting Time

6 min readUpdated September 2026
Calendar pages

"Add 30 days" and "add one month" are not the same thing, and mixing them up causes real scheduling bugs. A little awareness of how date arithmetic behaves keeps your due dates honest.

Month-end surprises

What is one month after January 31? There is no February 31, so tools clamp to the last valid day — February 28 or 29. That is sensible, but it means adding one month and then another does not always land where you expect. When precision matters, prefer counting in days, or verify the result explicitly.

Clamping is the near-universal convention, but it is not what naive code does. In JavaScript, setting the month of January 31 to February produces March 3, because the date object normalises an impossible value by rolling it forward rather than pulling it back. Two systems handling the same subscription can therefore disagree by three days, and neither of them raises an error.

Recurring billing needs one rule more. If each cycle is computed from the previous clamped date, a subscription that started on January 31 slides to February 28 and then stays on the 28th forever. Anchoring every cycle to the original day of the month instead gives February 28, March 31, April 30 — the sequence customers expect to see.

Watch the endpoints

"From the 1st to the 8th" is seven days if you count the gaps, but eight days if you count both endpoints (as with a hotel stay counted in nights vs days). Decide whether your count is inclusive before you trust it — off-by-one errors hide here more than anywhere else in date math.

The cleanest way to avoid the question is the half-open range: include the start, exclude the end. Written that way, the length of a range is simply end minus start, adjacent ranges tile without gaps or overlaps, and a hotel stay from 3 to 7 August is plainly four nights. Databases and APIs default to this convention for exactly those reasons.

Underneath it all is the fencepost problem: n points along a line enclose n minus 1 gaps. Ages, anniversaries and "which day of the trip is this" count points; durations, rentals and interest periods count gaps. Naming which of the two you mean, in the same sentence that states the rule, removes the ambiguity before anybody has to compute anything.

Keep time zones out of date-only math

When you only care about calendar dates, compare them at midnight rather than as full timestamps. Otherwise a daylight-saving shift can make a whole-day difference come out as 23 or 25 hours and round the wrong way. DateBuffet's tools compare date-only values at midnight to avoid exactly this.

The offsets themselves are less tidy than people expect. India runs at UTC+05:30, Nepal at UTC+05:45, the Chatham Islands at UTC+12:45, and Lord Howe Island shifts by only thirty minutes for daylight saving rather than a full hour. Arithmetic that quietly assumes whole-hour offsets produces results that look almost right, which is the hardest kind of mistake to notice.

Order of operations changes the answer

Date arithmetic stops being associative as soon as months are involved. Adding one month to January 30 and then one day gives March 1, while adding one day first and then one month gives February 28 — the same two operations, a day apart. Subtraction is not reversible either: one month before March 31 is February 28, and one month after that is March 28.

So fix the order in the rule itself, not in whichever code happens to implement it. "Three months after the invoice date, then the next business day" is unambiguous; "three months and five business days" is not. Where a sequence of adjustments is unavoidable, apply the coarsest unit first — years, then months, then days — which is the order most libraries use and the one readers assume.

Days and months are different kinds of quantity

A day is a fixed amount of elapsed time; a month is an instruction to move the calendar. That is why serious date libraries keep the two apart — java.time has Period for calendar amounts and Duration for elapsed time, and ISO 8601 writes P1M and P30D as genuinely different durations. Blurring them is what makes "30 days" and "one month" agree in April and disagree in eleven other months.

Contracts inherit the same distinction. "Net 30" and "payable within one month" produce different due dates in every month except April, June, September and November, and a quarterly obligation anchored to the 31st has no anchor at all in five months of the year. Whenever the unit is negotiable, choose days: it is the only one of the two with a constant length.

Dates before 1582 belong to another calendar

The Gregorian calendar began where 4 October 1582 was followed directly by 15 October 1582, in the countries that adopted it first. Britain and its colonies waited until September 1752, when 2 September was followed by 14 September; Russia changed in 1918 and Greece in 1923. A date from those centuries therefore has to say which calendar it is written in before it can be compared with anything.

Software almost always uses the proleptic Gregorian calendar, which extends today's rules backwards indefinitely, so a database happily stores 1500-01-01 even though nobody living then would have recognised the label. ISO 8601 acknowledges the problem by requiring the parties to agree before exchanging dates earlier than 1583. For historical work, record the calendar next to the date, or convert to Julian day numbers, which have no calendar at all.

Spreadsheets do their own date math

Excel stores a date as a serial number counting days, with 1 January 1900 as day 1, and deliberately includes a 29 February 1900 that never existed — a compatibility quirk inherited from Lotus 1-2-3. The consequence is that serial numbers before 1 March 1900 are one day out, so any difference spanning that date is wrong by one. Old Macintosh files add a second trap: the 1904 date system shifts every serial by 1,462 days.

Because dates are numbers there, subtracting two cells gives a dependable day count — which makes it the figure to compare when a spreadsheet and an application disagree. The month-aware functions are where trouble starts: Excel's DATEDIF is an undocumented holdover whose "MD" unit can return a negative number, and Microsoft's own guidance is not to use that unit.

The takeaway

Prefer days over months when precision matters, decide if your count is inclusive, and keep time zones out of date-only comparisons. The add/subtract and date-difference tools apply these rules for you.

Related tool
Add / Subtract Days

Add or subtract days, weeks, months or years from any date.