Reputation: 561
i want to disabled selected angular material between tow date . for example user can not selected any date between 2021-03-02
and 2011-03-02
.
i write this code :
<input matInput [max]="maxDate" [min]="minDate" [matDatepicker]="picker6"/>
maxDate = new Date();
minDate =new Date().setFullYear(this.maxDate.getFullYear() - 10)
but it not worked .
whats the problem ? how can i sovle this problem ???
Upvotes: 1
Views: 1180
Reputation: 1429
Please replace your html file with below
your.component.html
<mat-form-field>
<mat-label>Choose Start date</mat-label>
<input matInput [max]="unavailabilityForm.controls.startDate.value" [matDatepicker]="picker1" />
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker [dateClass]="dateClass" #picker1></mat-datepicker>
</mat-form-field>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Choose End date</mat-label>
<input matInput [min]="unavailabilityForm.controls.endDate.value" [matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker [dateClass]="dateClass" #picker></mat-datepicker>
</mat-form-field>
your.component.ts
import { Component, OnInit, ViewEncapsulation } from "@angular/core";
import { FormBuilder } from "@angular/forms";
/** @title Datepicker with custom date classes */
@Component({
selector: "datepicker-date-class-example",
templateUrl: "datepicker-date-class-example.html",
styleUrls: ["datepicker-date-class-example.css"],
encapsulation: ViewEncapsulation.None
})
export class DatepickerDateClassExample implements OnInit {
unavailabilityForm: any;
maxDate = new Date();
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
let startDateTimeStamp = new Date().setFullYear(new Date().getFullYear() - 10);
this.unavailabilityForm = this.formBuilder.group({
startDate: [new Date(startDateTimeStamp)],
endDate: [new Date()]
});
}
}
It will resolve your concern.
Upvotes: 1