{"record":{"id":"01eae0fe0c0de7c6","repo":"angular/components","slug":"invalid-date-date-date-has-to-be-greater-tha-01eae0","errorCode":null,"errorMessage":"Invalid date \"${date}\". Date has to be greater than 0.","messagePattern":"Invalid date \"(.+?)\"\\. Date has to be greater than 0\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material-moment-adapter/adapter/moment-date-adapter.ts","lineNumber":159,"sourceCode":"\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 {\n    return this._createMoment().locale(this.locale);\n  }\n\n  parse(value: unknown, parseFormat: string | string[]): Moment | null {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-moment-adapter/adapter/moment-date-adapter.ts#L141-L177","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use addCalendarDays(startOfMonth, -1) for relative-day math instead of passing 0 or negative dates.","Validate that date >= 1 before calling createDate.","Check calculations that produce day-of-month for off-by-one/underflow bugs.","If day 0 was meant to roll to the last day of the previous month, construct that month explicitly."],"exampleFix":"// before\nconst d = adapter.createDate(2024, 1, 0); // day 0\n// after\nconst d = adapter.addCalendarDays(adapter.createDate(2024, 1, 1), -1); // Jan 31, 2024","handlingStrategy":"validation","validationCode":"function canCreateDate(y: number, m: number, d: number): boolean {\n  return Number.isInteger(d) && d >= 1 && d <= 31 && m >= 0 && m <= 11;\n}\nif (!canCreateDate(y, m, d)) throw new Error('bad day-of-month: ' + d);","typeGuard":"function isValidDayOfMonth(d: unknown): d is number {\n  return typeof d === 'number' && Number.isInteger(d) && d >= 1;\n}","tryCatchPattern":"try {\n  return adapter.createDate(y, m, d);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Date has to be greater than 0')) {\n    return adapter.addCalendarDays(adapter.createDate(y, m, 1), d); // handle relative math\n  }\n  throw e;\n}","preventionTips":["Never pass 0 or negative day-of-month; use addCalendarDays for relative dates","Validate all date components coming from forms/APIs before constructing","Check arithmetic that can underflow (subtracting across month boundaries)","Cover underflow cases in unit tests"],"tags":["date-adapter","moment","validation","arguments"],"backgroundTag":"invalid-date-of-month","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}