{"record":{"id":"c692666b9dc0fdcc","repo":"angular/components","slug":"invalid-month-index-month-month-index-has-to-c69266","errorCode":null,"errorMessage":"Invalid month index \"${month}\". Month index has to be between 0 and 11.","messagePattern":"Invalid month index \"(.+?)\"\\. Month index has to be between 0 and 11\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material-moment-adapter/adapter/moment-date-adapter.ts","lineNumber":155,"sourceCode":"\n  getFirstDayOfWeek(): number {\n    return this._localeData.firstDayOfWeek;\n  }\n\n  getNumDaysInMonth(date: Moment): number {\n    return this.clone(date).daysInMonth();\n  }\n\n  clone(date: Moment): Moment {\n    return date.clone().locale(this.locale);\n  }\n\n  createDate(year: number, month: number, date: number): Moment {\n    // Moment.js will create an invalid date if any of the components are out of bounds, but we\n    // explicitly check each case so we can throw more descriptive errors.\n    if (typeof ngDevMode === 'undefined' || ngDevMode) {\n      if (month < 0 || month > 11) {\n        throw Error(`Invalid month index \"${month}\". Month index has to be between 0 and 11.`);\n      }\n\n      if (date < 1) {\n        throw Error(`Invalid date \"${date}\". Date has to be greater than 0.`);\n      }\n    }\n\n    const result = this._createMoment({year, month, date}).locale(this.locale);\n\n    // If the result isn't valid, the date must have been out of bounds for this month.\n    if (!result.isValid() && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n      throw Error(`Invalid date \"${date}\" for month with index \"${month}\".`);\n    }\n\n    return result;\n  }\n\n  today(): Moment {","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-moment-adapter/adapter/moment-date-adapter.ts#L137-L173","documentation":"MomentDateAdapter.createDate builds a Moment from year/month/day components. Moment silently produces invalid dates for out-of-bounds components, so the adapter explicitly checks month (0-11, JavaScript-style zero-based index) and throws this error to give a clearer message than an 'Invalid Date' downstream. The check runs only in ngDevMode.","triggerScenarios":"Calling createDate(year, month, date) with month < 0 or month > 11, most often by passing a 1-based month (January as 1) into the zero-based API, or from setLocale/newDate internal flows with bad data.","commonSituations":"Migrating from APIs that use 1-based months, storing month values from user pickers that are 1-based, off-by-one in loops building calendars, backend payloads returning 1-12 months.","solutions":["Subtract 1 from 1-based month values: createDate(2024, month - 1, day).","Validate month is 0-11 before calling createDate.","Ensure UI pickers/schemas consistently use zero-based months for this adapter.","Check any code that recently switched moment date handling to Angular Material for a base-0/base-1 mismatch."],"exampleFix":"// before\nconst date = adapter.createDate(2024, 3, 15); // intended March, passed 3 (zero-based = April)\n// after\nconst date = adapter.createDate(2024, 2, 15); // March in zero-based indexing","handlingStrategy":"validation","validationCode":"function canCreateMonth(month: number): boolean {\n  return Number.isInteger(month) && month >= 0 && month <= 11;\n}\nconst safeMonth = isOneBased(month) ? month - 1 : month;","typeGuard":"function isZeroBasedMonth(m: unknown): m is number {\n  return typeof m === 'number' && Number.isInteger(m) && m >= 0 && m <= 11;\n}","tryCatchPattern":"try {\n  return adapter.createDate(y, m, d);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Invalid month index')) {\n    return adapter.createDate(y, m - 1, d); // likely 1-based input\n  }\n  throw e;\n}","preventionTips":["Standardize on zero-based months across your date utilities when using Material adapters","Convert at the boundary from any 1-based source (backends, pickers)","Write a property test asserting createDate never receives month outside 0-11","Prefer adapter.date()/adapter.today() plus addCalendarMonths over hand-built components"],"tags":["date-adapter","moment","validation","zero-based-index"],"backgroundTag":"invalid-month-index","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}