{"record":{"id":"f49790f9dedc489c","repo":"angular/components","slug":"luxondateadapter-cannot-format-invalid-date","errorCode":null,"errorMessage":"LuxonDateAdapter: Cannot format invalid date.","messagePattern":"LuxonDateAdapter: Cannot format invalid date\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material-luxon-adapter/adapter/luxon-date-adapter.ts","lineNumber":210,"sourceCode":"          return fromFormat;\n        }\n      }\n\n      return this.invalid();\n    } else if (typeof value === 'number') {\n      return LuxonDateTime.fromMillis(value, options);\n    } else if (value instanceof Date) {\n      return LuxonDateTime.fromJSDate(value, options);\n    } else if (value instanceof LuxonDateTime) {\n      return LuxonDateTime.fromMillis(value.toMillis(), options);\n    }\n\n    return null;\n  }\n\n  format(date: LuxonDateTime, displayFormat: string): string {\n    if (!this.isValid(date)) {\n      throw Error('LuxonDateAdapter: Cannot format invalid date.');\n    }\n    if (this._useUTC) {\n      return date.setLocale(this.locale).setZone('utc').toFormat(displayFormat);\n    } else {\n      return date.setLocale(this.locale).toFormat(displayFormat);\n    }\n  }\n\n  addCalendarYears(date: LuxonDateTime, years: number): LuxonDateTime {\n    return date.reconfigure(this._getOptions()).plus({years});\n  }\n\n  addCalendarMonths(date: LuxonDateTime, months: number): LuxonDateTime {\n    return date.reconfigure(this._getOptions()).plus({months});\n  }\n\n  addCalendarDays(date: LuxonDateTime, days: number): LuxonDateTime {\n    return date.reconfigure(this._getOptions()).plus({days});","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-luxon-adapter/adapter/luxon-date-adapter.ts#L192-L228","documentation":"LuxonDateAdapter.format refuses to format an invalid LuxonDateTime, because Luxon would return the string 'Invalid DateTime' and silently corrupt the UI. The adapter throws a descriptive error instead, enforcing that only valid dates reach the display layer.","triggerScenarios":"Calling `adapter.format(date, displayFormat)` with a date that came from `LuxonDateTime.invalid(...)`, from a failed `fromFormat`/`fromISO` call, or from `adapter.createDate` logic bypassed manually; also when a date stored/retrieved from storage deserialized to an invalid DateTime.","commonSituations":"Displaying dates parsed from malformed user input without checking isValid; reviving persisted dates with JSON.parse (produces plain objects/invalid DateTimes); passing results of failed parse() calls straight into the datepicker's display pipeline.","solutions":["Check `adapter.isValid(date)` before formatting and render a placeholder for invalid values.","If the date came from JSON persistence, rehydrate with `LuxonDateTime.fromISO`/`fromMillis`, not JSON.parse.","Handle parse() returning null for unparseable input before using the result.","Catch this error at the display boundary and fall back to a fallback string like '—' or 'Invalid date'."],"exampleFix":"// before\nconst text = adapter.parse(userInput, 'yyyy-MM-dd');\ndisplay = adapter.format(text, 'dd/MM/yyyy'); // may throw\n\n// after\ndisplay = text && adapter.isValid(text) ? adapter.format(text, 'dd/MM/yyyy') : '—';","handlingStrategy":"type-guard","validationCode":"function safeFormat(adapter: DateAdapter<LuxonDateTime>, date: LuxonDateTime | null, fmt: string): string {\n  return date && adapter.isValid(date) ? adapter.format(date, fmt) : '—';\n}","typeGuard":"function isValidLuxonDate(adapter: DateAdapter<LuxonDateTime>, d: LuxonDateTime | null | undefined): d is LuxonDateTime {\n  return d != null && adapter.isValid(d);\n}","tryCatchPattern":"try {\n  return adapter.format(date, displayFormat);\n} catch (e) {\n  if (String(e.message).includes('Cannot format invalid date')) return 'Invalid date';\n  throw e;\n}","preventionTips":["Always check adapter.isValid() before formatting or binding dates to the UI.","Rehydrate persisted dates with LuxonDateTime.fromISO/fromMillis, never JSON.parse.","Treat a null return from parse() as 'no date', not an invalid date.","Centralize formatting in one helper that performs the validity check."],"tags":["angular-material","luxon","invalid-date","formatting"],"backgroundTag":"cannot-format-invalid-date","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}