{"record":{"id":"f002a739059a640d","repo":"angular/components","slug":"momentdateadapter-cannot-format-invalid-date","errorCode":null,"errorMessage":"MomentDateAdapter: Cannot format invalid date.","messagePattern":"MomentDateAdapter: Cannot format invalid date\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material-moment-adapter/adapter/moment-date-adapter.ts","lineNumber":187,"sourceCode":"\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 {\n    if (value && typeof value == 'string') {\n      return this._createMoment(value, parseFormat, this.locale);\n    }\n    return value ? this._createMoment(value).locale(this.locale) : null;\n  }\n\n  format(date: Moment, displayFormat: string): string {\n    date = this.clone(date);\n    if (!this.isValid(date) && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n      throw Error('MomentDateAdapter: Cannot format invalid date.');\n    }\n    return date.format(displayFormat);\n  }\n\n  addCalendarYears(date: Moment, years: number): Moment {\n    return this.clone(date).add({years});\n  }\n\n  addCalendarMonths(date: Moment, months: number): Moment {\n    return this.clone(date).add({months});\n  }\n\n  addCalendarDays(date: Moment, days: number): Moment {\n    return this.clone(date).add({days});\n  }\n\n  toIso8601(date: Moment): string {\n    return this.clone(date).format();","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material-moment-adapter/adapter/moment-date-adapter.ts#L169-L205","documentation":"MomentDateAdapter.format clones the date and refuses to format an invalid Moment, because formatting an invalid date would silently emit 'Invalid Date' or empty output. It throws this error in ngDevMode so the developer learns the underlying date is invalid (bad parse, invalid components) at the formatting call site.","triggerScenarios":"Calling format() with a Moment that failed to parse from a string, was built from invalid createDate input (production checks bypassed), or became invalid after invalid(null) / bad user input in a datepicker.","commonSituations":"Free-text date input parsed with strict mode failing, datepicker bound to garbage state, formatting a null-derived date, upstream regression where a createDate out-of-bounds check was skipped (prod builds).","solutions":["Check adapter.isValid(date) before calling format and handle the invalid branch (show placeholder/empty).","Trace why the date is invalid — inspect date.creationData() / invalidReason() on the Moment.","Use a parse fallback: if adapter.parse fails, fall back to today or null rather than formatting the invalid result.","Verify you are not passing a raw string to format; deserialize first with adapter.deserialize/fromISOString."],"exampleFix":"// before\nconst label = adapter.format(userDate, 'YYYY-MM-DD'); // userDate may be invalid\n// after\nconst label = adapter.isValid(userDate) ? adapter.format(userDate, 'YYYY-MM-DD') : '';","handlingStrategy":"type-guard","validationCode":"if (!adapter.isValid(date)) {\n  return '';\n}\nconst label = adapter.format(date, displayFormat);","typeGuard":"function isFormattable(adapter: MomentDateAdapter, date: Moment | null | undefined): date is Moment {\n  return !!date && adapter.isValid(date);\n}","tryCatchPattern":"try {\n  return adapter.format(date, fmt);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Cannot format invalid date')) {\n    console.warn('invalid moment', (date as any)?.invalidReason?.());\n    return '';\n  }\n  throw e;\n}","preventionTips":["Always check adapter.isValid before format","Inspect moment.invalidReason()/creationData() to find parse failures","Return null/'' from failed parses instead of propagating invalid Moments","Gate format calls behind a typed helper that accepts only valid dates"],"tags":["date-adapter","moment","formatting","invalid-date"],"backgroundTag":"cannot-format-invalid-date","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}