Lorixen.
EN

Monthly billing that starts on the 31st: avoid drifting dates

Short answer: "Add one month" is ambiguous when a subscription starts on the 29th, 30th or 31st. Some code rolls January 31 over into early March, other code clamps it to February 28 and then keeps renewing on the 28th forever. The reliable approach is to store the original billing day as an anchor and calculate each renewal from the anchor, using the last day of the month when the anchor day doesn't exist. Payment platforms such as Stripe already work this way, so if you keep your own dates, match them.

Three answers to one question

A customer signs up on January 31 for a monthly plan. When is the next renewal?

  • Roll over: MDN's documentation for JavaScript's setMonth gives exactly this example: with the date set to 31 January 2016, setting the month to February returns 2 March 2016, because February only had 29 days and the extra days spill into March. Code that does date.setMonth(date.getMonth() + 1) skips February entirely.
  • Clamp: PostgreSQL's documentation explains that adding months keeps the day of the month unless it would be past the end of the new month, in which case the last day is used. So January 31 plus one month is February 28 (or 29), and March 31 plus one month is April 30.
  • Anchor to month end: Stripe's billing documentation describes a monthly subscription anchored on January 31 billing on the last day of the month closest to the anchor: February 28 (or 29 in a leap year), then March 31, April 30, and so on.

All three are "one month later" by some definition. Only one matches what most customers expect.

The drift problem

Clamping on its own is fine for a single step. The trouble is repeated steps. If your code computes each renewal from the previous renewal:

  • January 31 → February 28
  • February 28 → March 28
  • March 28 → April 28 … forever

The customer who signed up on the 31st now renews on the 28th for the rest of their life, and their billing periods don't match "monthly" in your reports. PostgreSQL's own example shows the difference: March 31 plus one month is April 30, but March 31 plus two months is May 31. Adding months to the original date avoids drift; adding one month at a time doesn't.

The fix: calculate from the anchor

Store two things:

  • the anchor date (or just the anchor day of the month, such as 31)
  • the number of periods elapsed, or calculate the next one from today

Then each renewal is: anchor plus N months, using the last day of the month if the anchor day doesn't exist. In SQL, anchor + (n || ' months')::interval does this in PostgreSQL. In application code, use a date library's "add months" function on the original anchor, not on the last renewal, and check it clamps rather than rolls over.

Stripe also lets you choose an anchor day explicitly; its docs describe setting the day of month to 31 for subscriptions that should renew at the end of each month.

Match your payment platform

If a payment platform runs your subscriptions, it decides when customers are charged. Your app's "next renewal" dates, reminder emails and access periods should be read from the platform, or calculated with exactly the same rule. Two systems with different rules mean reminders arriving after the charge, or access ending a day early.

A simpler business option

Some businesses avoid the issue by limiting billing days, for example anchoring anyone who signs up on the 29th–31st to the 1st of the next month, with a small prorated first charge, or billing everyone on the same day. It's simpler for customers and for accounting. Decide deliberately and explain it at sign-up.

Show customers the date

Whatever rule you choose, show the next billing date plainly in the account area and on receipts, and use the same date in reminder emails. Customers rarely mind a renewal on the 30th instead of the 31st; they mind not knowing when it will happen, or seeing three different dates in three places. A visible date also lets support staff answer questions without digging through code or payment records.

Time zones and "the end of the day"

MDN also notes that setMonth works in local time and can cross daylight saving changes. Store billing anchors as dates, not local timestamps, and decide which time zone defines "the 31st" for a customer, usually your business's or the customer's, consistently.

Test cases to include

Add these to your tests, for any code that handles monthly dates:

  • sign-ups on the 28th, 29th, 30th and 31st
  • February in leap and non-leap years
  • twelve consecutive renewals from January 31, checking none drift
  • annual plans starting February 29
  • a customer who changes plan mid-period

Also check reminder emails ("your plan renews in 3 days") and grace periods, which often use their own date logic.

A worked example

An illustrative Columbus gym's membership app calculated renewals by adding one month to the last renewal date. Members who joined on the 30th or 31st gradually shifted to the 28th, while the payment platform kept charging them on the last day of the month. Reminder emails arrived after charges and the front desk saw "expired" members who had paid. The developer stored each member's anchor date, calculated renewals from it with month-end clamping, read the actual next charge date from the payment platform, and added tests for month-end sign-ups.

FAQ

Is rolling over into March ever right?

Rarely for billing. It usually surprises customers and skips a month.

Should I bill every 30 days instead?

That's a different product ("every 30 days"), and renewal days will move around the calendar. Say so clearly if you choose it.

What about weekly or yearly plans?

Weekly plans have no month-end issue; yearly plans have the February 29 case.

Do payment platforms all behave the same?

Not necessarily. Check your platform's documentation for month-end behaviour.

If your subscription dates are drifting or disagreeing with your payment platform, describe how renewals are calculated and we'll help you straighten them out.

Sources

WhatsAppBook a call