nextBillingDate function

DateTime nextBillingDate(
  1. DateTime from,
  2. int anchorDay
)

The next billing date strictly on or after the date part of from for anchorDay.

If from's date is already the (clamped) billing day of its own month, that same date is returned; otherwise the search advances to the next month whose clamped anchor is on/after from. anchorDay must be 1..31.

Example:

// Anchor 15, from the 20th -> next month's 15th.
nextBillingDate(DateTime(2026, 3, 20), 15); // 2026-04-15

Audited: 2026-06-12 11:26 EDT

Implementation

DateTime nextBillingDate(DateTime from, int anchorDay) {
  _checkAnchor(anchorDay);

  final DateTime fromDate = _dateOnly(from);
  final DateTime thisMonth = billingDateInMonth(fromDate.year, fromDate.month, anchorDay);

  // This month's billing date already satisfies "on or after from".
  if (!thisMonth.isBefore(fromDate)) {
    return thisMonth;
  }
  return billingDateInMonth(fromDate.year, fromDate.month + 1, anchorDay);
}