Search code examples
angulardateangular-material

Min Date and Max Date not working Angular Material Range Input


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.


Solution

  • 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