MatDatepicker returns null or invalid date without DateAdapter
0 reputation · 10 Dec 2020, 08:53 UTC
0 reputation · 10 Dec 2020, 08:53 UTC
The goal is to bind an existing date value to an Angular Material MatDatepicker, allow the user to pick a new date, and write the result back to the same form model. Angular Material does not supply a default DateAdapter, so the component requires an explicit provider such as MatNativeDateModule, MatMomentDateModule, MatLuxonDateModule, or a custom DateAdapter.
Behavior varies by Angular Material major version. MatNativeDateModule is deprecated in recent releases, and native Date parsing depends on the browser timezone, which can shift recovered date values. The selected adapter controls parsing, formatting, and serialization, so identical input can produce null or an invalid date depending on the module and version.
Behavior must be verified against the installed Angular Material release.
Which DateAdapter provider matches the installed Angular Material major version? Should Moment.js, Luxon, or native Date be used to prevent timezone shifts in recovered values? What serialization format preserves the original date after selection and form submission?
37275 reputation · 10 Dec 2020, 13:35 UTC
The picker depends on a DateAdapter to parse, format and serialize dates. In Angular Material 14 and later MatNativeDateModule is deprecated, so a dedicated adapter must be supplied. Using the browser’s native Date without an adapter causes timezone‑dependent off‑by‑one errors, resulting in null or invalid values.
If you are on an older Angular Material release (13 or earlier) MatNativeDateModule is still supported, but verify that the browser’s timezone settings remain stable.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 10 Dec 2020, 16:49 UTC
For calendar-date models, serialize as YYYY-MM-DD strings in the form model rather than JavaScript Date objects. JSON serialization of Date always emits UTC ISO strings, which shifts the day when the browser timezone offset crosses midnight. A date-only string avoids this entirely and works with any adapter.
Adapter choice should match the installed @angular/material major version: MatNativeDateModule is deprecated from v15+; MatLuxonDateModule (via @angular/material-luxon-adapter) is the lightest actively maintained option with IANA timezone support. MatMomentDateModule works but adds ~16 kB gzipped and Moment is in maintenance mode.
On submit, normalize the picker value with adapter.format(date, 'YYYY-MM-DD') instead of trusting raw output — selection can emit adapter-dependent objects, strings, or null when parsing fails, not only on cancellation.