Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 998
Alezco05 Avatar asked Aug 18 '20 20:08

Alezco05


People also ask

How do I use mat datepicker in angular?

Datepicker in Angular using mat-datepicker | Material Design. To implement date picker in Angular we can use angular material datepicker module called MatDatepickerModule. Date picker is a component which allow users to choose a date from the calendar pop up or enter a date through text input.

How to validate minimum and maximum date?

The error message will be displayed if required validation fails. To validate minimum and maximum date entered by user manually or selected from calendar, can be validated using min and max property.

What's new in angular 10?

Recently, Angular 10 was released. In this release, Angular Material 10 came up with new date range picker control, which is easy to use and very helpful to implement above functionalities. In this article, we will see how to use the Angular Material date range picker, it's integration with forms, and usage of its important properties.

How to use mat-date-range-picker component in formcontrol?

mat-date-range-input component requires the two input boxes, which will allow us to manually enter the start date and end date in input boxes, and with these controls we will map the FormControl. mat-date-range-picker component is the calender popup panel which will allow us to select the date range.


1 Answers

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

like image 67
aviboy2006 Avatar answered Sep 17 '22 13:09

aviboy2006