I have an array of Dates that I call holidayList that I'd like to disable using Angular Material's Datepicker.
holidayList: Date[] = [];
this.holidayList: [
new Date("1/1/2020"),
new Date("1/20/2020"),
new Date("2/17/2020"),
new Date("5/25/2020"),
new Date("7/4/2020"),
new Date("9/7/2020"),
new Date("10/12/2020"),
new Date("11/11/2020"),
new Date("11/26/2020"),
new Date("12/25/2020")
]
Reading Angular's Date validation documentation, we can utilize [matDatepickerFilter]="myFilter"
like so:
HTML:
<mat-form-field class="example-full-width">
<input matInput [matDatepickerFilter]="myFilter" [matDatepicker]="picker" placeholder="Choose a date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
TypeScript:
import {Component} from '@angular/core';
/** @title Datepicker with filter validation */
@Component({
selector: 'datepicker-filter-example',
templateUrl: 'datepicker-filter-example.html',
styleUrls: ['datepicker-filter-example.css'],
})
export class DatepickerFilterExample {
myFilter = (d: Date): boolean => {
const day = d.getDay();
// Prevent Saturday and Sunday from being selected.
return day !== 0 && day !== 6;
}
}
However, this example provided is essentially saying 'provide all the days where the day of the week is not 0 or 6'. I am a bit confused as to how to implement a list of dates and say 'here are the list of dates we should disable'.
Link to Angular Material's example on Stackblitz
I know there are multiple questions on this topic but all the answers I've come across only address disabling certain days of the week, months (e.g disabling odd months) - essentially disabling a type of day; in this case, I'd like to disable hard-coded dates and nothing more.