{"record":{"id":"bae2bdfb9c38685a","repo":"angular/components","slug":"invalid-month-index-month-month-index-has-to","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":null,"httpStatus":null,"severity":"error","filePath":"src/material-date-fns-adapter/adapter/date-fns-adapter.ts","lineNumber":136,"sourceCode":"\n  getFirstDayOfWeek(): number {\n    return this.locale.options?.weekStartsOn ?? 0;\n  }\n\n  getNumDaysInMonth(date: Date): number {\n    return getDaysInMonth(date);\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    // Passing the year to the constructor causes year numbers <100 to be converted to 19xx.\n    // To work around this we use `setFullYear` and `setHours` instead.\n    const result = new Date();\n    result.setFullYear(year, month, date);\n    result.setHours(0, 0, 0, 0);\n\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","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-date-fns-adapter/adapter/date-fns-adapter.ts#L118-L154","documentation":"DateFnsAdapter.createDate validates the month argument and throws when it falls outside 0–11 (date-fns/Material use zero-based months: 0=January, 11=December). The check runs in dev mode. This prevents silently rolling the date into the next year/month via Date overflow.","triggerScenarios":"Calling adapter.createDate(year, month, date) with month < 0 or month > 11 — e.g. passing a human month number like 12 for December instead of 11, or -1 when computing previous month by naive subtraction.","commonSituations":"Converting from one-based user input (month pickers, forms) without subtracting 1; month arithmetic like currentMonth - 1 going below 0 without wrapping; mixing DateAdapter implementations with different conventions.","solutions":["Subtract 1 from one-based month values before calling createDate.","Wrap month arithmetic with modulo: (month + 11) % 12 when decrementing.","Use date-fns helpers like setMonth/addMonths instead of raw indices.","Validate user-supplied month input against 1–12 before conversion."],"exampleFix":"// before\nthis.adapter.createDate(2024, 12, 25); // intended December, throws\n// after\nthis.adapter.createDate(2024, 11, 25); // zero-based: 11 = 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  return this.adapter.createDate(year, month, day);\n} catch (e) {\n  if ((e as Error).message.startsWith('Invalid month index')) {\n    return this.adapter.createDate(year, Math.min(Math.max(month, 0), 11), day);\n  }\n  throw e;\n}","preventionTips":["Always convert one-based months (form/UI input) with month - 1","Use (m + 11) % 12 wrapping for previous-month arithmetic","Prefer adapter methods (addCalendarMonths, setMonth equivalents) over raw indices","Document zero-based convention wherever createDate is called"],"tags":["date","validation","material","date-fns","dev-mode"],"backgroundTag":"invalid-date-argument","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}