{"record":{"id":"4941f9d837e64a53","repo":"angular/components","slug":"invalid-month-index-month-month-index-has-to-4941f9","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/core/datetime/native-date-adapter.ts","lineNumber":133,"sourceCode":"    return 0;\n  }\n\n  getNumDaysInMonth(date: Date): number {\n    return this.getDate(\n      this._createDateWithOverflow(this.getYear(date), this.getMonth(date) + 1, 0),\n    );\n  }\n\n  clone(date: Date): Date {\n    return new Date(date.getTime());\n  }\n\n  createDate(year: number, month: number, date: number): Date {\n    if (typeof ngDevMode === 'undefined' || ngDevMode) {\n      // Check for invalid month and date (except upper bound on date which we have to check after\n      // creating the Date).\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    let result = this._createDateWithOverflow(year, month, date);\n    // Check that the date wasn't above the upper bound for the month, causing the month to overflow\n    if (result.getMonth() != month && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n      throw Error(`Invalid date \"${date}\" for month with index \"${month}\".`);\n    }\n\n    return result;\n  }\n\n  today(): Date {\n    return new Date();","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material/core/datetime/native-date-adapter.ts#L115-L151","documentation":"NativeDateAdapter.createDate validates inputs in dev mode and rejects month indexes outside 0-11 (JS Date months are zero-based). Throwing early prevents silent Date rollover where month 12 would become January of the next year.","triggerScenarios":"Calling adapter.createDate(year, month, date) with month < 0 or month > 11 — e.g. passing human month numbers 1-12 instead of 0-11, or out-of-range computed values.","commonSituations":"Developers converting UI month pickers (1-12) directly to createDate; porting code from libraries using 1-based months; arithmetic like month + 1 applied twice producing 12.","solutions":["Convert 1-based months to zero-based: createDate(year, month - 1, date).","Clamp/validate month input to 0..11 before calling createDate.","When advancing months, use adapter.addCalendarMonths which handles rollover instead of manual arithmetic."],"exampleFix":"// before\nconst d = adapter.createDate(2026, 12, 15); // human December\n// after\nconst d = adapter.createDate(2026, 11, 15); // zero-based December","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(month) || month < 0 || month > 11) {\n  throw new RangeError(`month must be 0-11, got ${month}`);\n}\nconst d = adapter.createDate(year, month, date);","typeGuard":"function isValidMonthIndex(m: unknown): m is number {\n  return typeof m === 'number' && Number.isInteger(m) && m >= 0 && m <= 11;\n}","tryCatchPattern":"try {\n  d = adapter.createDate(year, month, day);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Invalid month index')) {\n    d = adapter.createDate(year, Math.min(Math.max(month, 0), 11), day);\n  } else { throw e; }\n}","preventionTips":["Always store months zero-based (0-11) in app state.","Convert 1-based UI month with month - 1.","Prefer adapter.addCalendarMonths over manual month arithmetic."],"tags":["angular-material","date-adapter","date-validation","month-index"],"backgroundTag":"invalid-month-index","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}