{"record":{"id":"e00391729edc2498","repo":"angular/components","slug":"invalid-date-date-date-has-to-be-greater-tha","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":null,"httpStatus":null,"severity":"error","filePath":"src/material-date-fns-adapter/adapter/date-fns-adapter.ts","lineNumber":140,"sourceCode":"\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\n    return result;\n  }\n\n  today(): Date {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-date-fns-adapter/adapter/date-fns-adapter.ts#L122-L158","documentation":"DateFnsAdapter.createDate throws when the day-of-month argument is less than 1. Day 0 or negative days are invalid and would silently shift into the previous month if passed to the native Date, so the adapter rejects them early (dev mode).","triggerScenarios":"Calling createDate(year, month, date) with date < 1 — e.g. date arithmetic like dayOfMonth - 1 producing 0 when computing the previous day, or parsing unvalidated user input containing 0.","commonSituations":"Naive previous-day calculation hitting day 0; empty or malformed form fields yielding 0; off-by-one errors in calendar grid generation.","solutions":["Ensure day values are >= 1; clamp or validate before calling createDate.","For previous/next day arithmetic use addDays instead of decrementing raw day numbers.","Validate form input so empty/zero values never reach createDate."],"exampleFix":"// before\nconst prevDay = this.adapter.createDate(2024, 0, day - 1); // day=1 -> date=0, throws\n// after\nconst prevDay = this.adapter.addCalendarDays(this.adapter.createDate(2024, 0, day), -1);","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(date) || date < 1) {\n  throw new RangeError(`date must be >= 1, got ${date}`);\n}\nconst d = adapter.createDate(year, month, date);","typeGuard":"function isValidDayOfMonth(d: unknown): d is number {\n  return typeof d === 'number' && Number.isInteger(d) && d >= 1 && d <= 31;\n}","tryCatchPattern":"try {\n  return this.adapter.createDate(year, month, day);\n} catch (e) {\n  if ((e as Error).message.startsWith('Invalid date \"')) {\n    return this.adapter.today(); // or clamp day to 1\n  }\n  throw e;\n}","preventionTips":["Reject day 0/negatives from forms before building dates","Use addCalendarDays for relative day shifts instead of decrementing day numbers","Sanitize empty/NaN inputs to a default before createDate"],"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-08-31T14:17:45.589Z"}