billingSchedule function

List<DateTime> billingSchedule(
  1. DateTime start,
  2. int anchorDay,
  3. int count
)

count consecutive monthly billing dates beginning with the next billing date on/after start, each clamped to its month's length.

anchorDay must be 1..31 and count must be >= 0 (a zero count yields an empty list).

Example:

// Three billings from Jan 31 2024, showing the Feb clamp.
billingSchedule(DateTime(2024, 1, 31), 31, 3);
// [2024-01-31, 2024-02-29, 2024-03-31]

Audited: 2026-06-12 11:26 EDT

Implementation

List<DateTime> billingSchedule(DateTime start, int anchorDay, int count) {
  _checkAnchor(anchorDay);
  if (count < 0) {
    throw ArgumentError.value(count, 'count', 'must be >= 0');
  }

  final List<DateTime> dates = <DateTime>[];
  DateTime current = nextBillingDate(start, anchorDay);

  // Step a whole calendar month each iteration by re-clamping the anchor against
  // the FOLLOWING month, never by adding 30/31 days (which would drift the day).
  for (int i = 0; i < count; i++) {
    dates.add(current);
    current = billingDateInMonth(current.year, current.month + 1, anchorDay);
  }
  return dates;
}