Angular Material 10 range datepicker and moment.js Error: date.getFullYear is not a function

Viewed 965

Trying to implement Angular Material v10 datepicker with the rangePicker option with moment.js But when I use moment in combination with the rangePicker it gives me this error.

Error: date.getFullYear is not a function

This error happens when I try to select the second date matEndDate

Modified Google Stackblitz example showing the error:

https://stackblitz.com/edit/angular-imb7gg?file=src/app/datepicker-moment-example.ts

1 Answers

Range datepicker uses range strategy with wrong DateAdapter.

In order to fix it you should provide MAT_DATE_RANGE_SELECTION_STRATEGY token on the same level where you provided custom DateAdapter:

import {
  MAT_DATE_RANGE_SELECTION_STRATEGY,
  DefaultMatCalendarRangeStrategy
} from '@angular/material/datepicker';
...
providers: [
 { provide: MAT_DATE_RANGE_SELECTION_STRATEGY, useClass: DefaultMatCalendarRangeStrategy},
 { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
 ....

It makes sure that DefaultMatCalendarRangeStrategy class will get MomentDateAdapter as a DateAdapter implementation instead of NativeDateAdapter

Forked Stackblitz

Related