Reputation: 9
I have a requirement to show the dates for start date and end date i.e., if the start date is dd/mm/yyyy
format 10/09/2020
and end date should be till yesterday i.e., 09/09/2020
and all the remaining dates should be disabled.
How can I achieve this?
<mat-form-field color="accent" appearance="fill">
<mat-label>Start Date</mat-label>
<input matInput [matDatepicker]="picker1" [max]="tomorrow" [formControl]="startDate">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
<mat-form-field color="accent" appearance="fill">
<mat-label>End Date</mat-label>
<input matInput [matDatepicker]="picker2" [min]="today" max="tomorrow" [formControl]="endDate">
<mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2></mat-datepicker>
</mat-form-field>
Upvotes: 0
Views: 1882
Reputation: 58039
May I want to say. In .ts
today=new Date();
tomorrow=new Date(this.today.getTime()+24*60*60*1000);
in .html
<input matInput [matDatepicker]="picker" [min]="today" [max]="tomorrow">
Upvotes: 0
Reputation: 474
You can use filter validation.
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Choose a date</mat-label>
<input matInput [matDatepickerFilter]="myFilter" [matDatepicker]="picker">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
import {Component} from '@angular/core';
/** @title Datepicker with filter validation */
@Component({
selector: 'datepicker-filter-example',
templateUrl: 'datepicker-filter-example.html',
})
export class DatepickerFilterExample {
myFilter = (d: Date | null): boolean => {
const day = (d || new Date()).getDay();
// Prevent Saturday and Sunday from being selected.
return day !== 0 && day !== 6;
}
}
Example from the offical docs here
Upvotes: 1