Min Date and Max Date not working Angular Material Range Input
Asked Answered
N

2

22

I have this simple form which is a range input from angular material. But the [min] and [max] date are not working. I tried to put in the first input, the second input and then both of them, but nothing works. I leave my code

 <mat-form-field appearance="outline" class="w-100">
        <mat-date-range-input [rangePicker]="picker" >
          <input matStartDate placeholder="Start date" formControlName="start" [min]="minDate" 
           [max]="maxDate">
          <input matEndDate placeholder="End date" formControlName="end" [min]="minDate" 
           [max]="maxDate">
        </mat-date-range-input>
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-date-range-picker #picker></mat-date-range-picker>
      </mat-form-field>

I'm trying to get the max to be the same day, I mean today.

export class DateComponent implements OnInit, {
  minDate: Date;
  maxDate: Date;
 constructor(private formBuilder: FormBuilder) {
    const currentYear = new Date().getFullYear();
    this.minDate = new Date(currentYear - 20, 0, 1);
    this.maxDate = new Date();
    }
}

But it's not working.

Noelyn answered 18/8, 2020 at 20:14 Comment(0)
P
41

Stackblitz : https://stackblitz.com/edit/angular-g3nnqx?file=src/app/datepicker-min-max-example.html

<mat-form-field appearance="fill" >
        <mat-date-range-input [rangePicker]="picker" [min]="minDate" 
           [max]="maxDate" >
          <input matStartDate placeholder="Start date"    >
          <input matEndDate placeholder="End date"   >
        </mat-date-range-input>
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-date-range-picker #picker></mat-date-range-picker>
      </mat-form-field>

When you are using rangePicker you can't apply min and max to input these are particular picker level. You have to set like you set rangePicker

More reference details : https://material.angular.io/components/datepicker/overview

Panther answered 18/8, 2020 at 20:47 Comment(0)
S
0
<mat-date-range-input [rangePicker]="picker" [dateFilter]="filterDate">

this is the another working answer for more complex scenarios

Seumas answered 3/5 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.