Alex Imenwo
Alex Imenwo

Reputation: 99

How to disable past date on angular material datepicker range

Below are samples for both template and component file. Using [min] property does not bind to the form input on the template. Any help will be very much appreaciated.

<mat-form-field>
  <mat-label>Enter a date range</mat-label>
  <mat-date-range-input [formGroup]="range" [rangePicker]="picker">
    <input matStartDate formControlName="start" placeholder="Start date" [min]="minDate">
    <input matEndDate formControlName="end" 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-error *ngIf="range.controls.start.hasError('matStartDateInvalid')">Invalid start date</mat-error>
  <mat-error *ngIf="range.controls.end.hasError('matEndDateInvalid')">Invalid end date</mat-error>
</mat-form-field>


import {Component} from '@angular/core';
import {FormGroup, FormControl} from '@angular/forms';

/** @title Date range picker forms integration */
@Component({
  selector: 'date-range-picker-forms-example',
  templateUrl: 'date-range-picker-forms-example.html',
})
export class DateRangePickerFormsExample {
  minDate: Date;
  range = new FormGroup({
    start: new FormControl(),
    end: new FormControl()
  });

 constructor() {
    const currentYear = new Date().getFullYear();
    this.minDate = new Date(currentYear - 0, 0, 1);
  }
}

Upvotes: 1

Views: 6406

Answers (2)

Samuel Mazahery
Samuel Mazahery

Reputation: 352

set minDate to currentDate

constructor() {
    this.minDate = new Date();
 }

Upvotes: 0

penleychan
penleychan

Reputation: 5470

You're using mat-date-range-input you can't apply min and max to input. You need to apply that on your mat-date-range-input

<mat-date-range-input [formGroup]="range" [rangePicker]="picker" [min]="minDate">
...
</mat-date-range-input>

Upvotes: 2

Related Questions