angular/components · error · Error

Invalid date "${date}". Date has to be greater than 0.

Error message

Invalid date "${date}". Date has to be greater than 0.

What it means

MomentDateAdapter.createDate rejects date-of-month values less than 1. Moment itself would silently build an invalid Moment (e.g. day 0 rolls over), so the adapter pre-checks and throws this explicit error in ngDevMode to surface the bad component early.

Source

Thrown at src/material-moment-adapter/adapter/moment-date-adapter.ts:159

  getNumDaysInMonth(date: Moment): number {
    return this.clone(date).daysInMonth();
  }

  clone(date: Moment): Moment {
    return date.clone().locale(this.locale);
  }

  createDate(year: number, month: number, date: number): Moment {
    // Moment.js will create an invalid date if any of the components are out of bounds, but we
    // explicitly check each case so we can throw more descriptive errors.
    if (typeof ngDevMode === 'undefined' || ngDevMode) {
      if (month < 0 || month > 11) {
        throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
      }

      if (date < 1) {
        throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
      }
    }

    const result = this._createMoment({year, month, date}).locale(this.locale);

    // If the result isn't valid, the date must have been out of bounds for this month.
    if (!result.isValid() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
      throw Error(`Invalid date "${date}" for month with index "${month}".`);
    }

    return result;
  }

  today(): Moment {
    return this._createMoment().locale(this.locale);
  }

  parse(value: unknown, parseFormat: string | string[]): Moment | null {

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Use addCalendarDays(startOfMonth, -1) for relative-day math instead of passing 0 or negative dates.
  2. Validate that date >= 1 before calling createDate.
  3. Check calculations that produce day-of-month for off-by-one/underflow bugs.
  4. If day 0 was meant to roll to the last day of the previous month, construct that month explicitly.

Example fix

// before
const d = adapter.createDate(2024, 1, 0); // day 0
// after
const d = adapter.addCalendarDays(adapter.createDate(2024, 1, 1), -1); // Jan 31, 2024
Defensive patterns

Strategy: validation

Validate before calling

function canCreateDate(y: number, m: number, d: number): boolean {
  return Number.isInteger(d) && d >= 1 && d <= 31 && m >= 0 && m <= 11;
}
if (!canCreateDate(y, m, d)) throw new Error('bad day-of-month: ' + d);

Type guard

function isValidDayOfMonth(d: unknown): d is number {
  return typeof d === 'number' && Number.isInteger(d) && d >= 1;
}

Try / catch

try {
  return adapter.createDate(y, m, d);
} catch (e) {
  if (e instanceof Error && e.message.includes('Date has to be greater than 0')) {
    return adapter.addCalendarDays(adapter.createDate(y, m, 1), d); // handle relative math
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling createDate(year, month, date) with date < 1, e.g. passing 0 for 'first day of month' or a computed day-of-month that underflowed to 0 or a negative value.

Common situations: Computing 'day before the 1st' by simple subtraction, day-of-month math with UTC/local drift producing 0, unvalidated form inputs, misusing createDate when addCalendarDays was intended.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/01eae0fe0c0de7c6. Report an issue: GitHub.