Why Monthly Billing Dates Drift on the 31st (And How Anchor Clamping Fixes Recurring Invoices)
If you automate recurring billing or subscription invoices in JavaScript, one subtle bug breaks client billing schedules almost immediately:
// Broken naive approach: const date = new Date("2026-01-31T00:00:00Z"); date.setUTCMonth(date.getUTCMonth() + 1); // -> 2026-03-03 (overflowed!) Even when libraries clamp to the end of the month (Jan 31 -> Feb 28), mutating that date sequentially causes permanent date drift: March bills on Mar 28, April bills on Apr 28, and every subsequent invoice stays stuck on the 28th forever instead of snapping back to the 31st.
The Solution: Stateless Anchor Clamping
Instead of stepping month-by-month from the previous result, calculate cycle n directly from the original contract anchor date (anchorDay = 31) and clamp to min(anchorDay, daysInTargetMonth):
export function addAnchoredMonthsUtc(anchorDateStr, monthsToAdd) { const [y, m, anchorDay] = anchorDateStr.split("-").map(Number); const totalMonths = y * 12 + (m - 1) + monthsToAdd; const targetYear = Math.floor(totalMonths / 12); const targetMonth = totalMonths % 12; const lastDay = new Date(Date.UTC(targetYear, targetMonth + 1, 0)).getUTCDate(); const clampedDay = Math.min(anchorDay, lastDay); return new Date(Date.UTC(targetYear, targetMonth, clampedDay)) .toISOString() .slice(0, 10); } With anchor clamping from 2026-01-31:
- Cycle 1:
2026-01-31 - Cycle 2:
2026-02-28 - Cycle 3:
2026-03-31(snaps cleanly back to the 31st) - Cycle 4:
2026-04-30
Handling Banking-Day Weekend Rollover & 28-Day Retainers
Two edge cases show up right alongside month-end clamping:
- Weekend payment deadlines: When Net 14 or Net 30 falls on a Saturday or Sunday, bank transfers and ACH batches stall until Monday. Rolling weekend due dates forward to the following Monday keeps aging reports accurate. You can preview weekend rollovers across Net 15, Net 30, Net 60, and EOM terms using our free Invoice Due Date Calculator.
- Monthly (12 cycles) vs. 28-day cycles (13 cycles): Agencies billing every 4 weeks generate 13 invoices per year instead of 12. You can compare both schedules and export a 12-to-36-month CSV with our browser-side Billing Cycle Calculator.
Open Source Package & Free Invoice Studio
We open-sourced the clean zero-dependency helper functions (anchor clamping, Net-term due-date rollover, 2/10 Net 30 discount APR, and UK statutory late-payment interest) under the MIT license at github.com/invoicycle/invoice-billing-utils.
If you need to turn those schedules into client-ready PDFs or automated recurring invoices without paid tiers blocking basic workflows, you can also draft and download invoices immediately inside Invoicycle's free invoice builder.