简体   繁体   中英

how can disable angular material date picker between to date

i want to disabled selected angular material between tow date. for example user can not selected any date between 2021-03-02 and 2011-03-02 .

i write this code:

 <input matInput [max]="maxDate" [min]="minDate" [matDatepicker]="picker6"/>

maxDate = new Date();
minDate =new Date().setFullYear(this.maxDate.getFullYear() - 10) 

but it not worked.

Demo

whats the problem? how can i sovle this problem???

Please replace your html file with below

your.component.html

<mat-form-field>
    <mat-label>Choose Start date</mat-label>
    <input matInput [max]="unavailabilityForm.controls.startDate.value" [matDatepicker]="picker1" />
    <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
    <mat-datepicker [dateClass]="dateClass" #picker1></mat-datepicker>
</mat-form-field>

<mat-form-field class="example-full-width" appearance="fill">
    <mat-label>Choose End date</mat-label>
    <input matInput [min]="unavailabilityForm.controls.endDate.value" [matDatepicker]="picker" />
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker [dateClass]="dateClass" #picker></mat-datepicker>
</mat-form-field>

your.component.ts

import { Component, OnInit, ViewEncapsulation } from "@angular/core";
import { FormBuilder } from "@angular/forms";

/** @title Datepicker with custom date classes */
@Component({
  selector: "datepicker-date-class-example",
  templateUrl: "datepicker-date-class-example.html",
  styleUrls: ["datepicker-date-class-example.css"],
  encapsulation: ViewEncapsulation.None
})
export class DatepickerDateClassExample implements OnInit {
  unavailabilityForm: any;
  maxDate = new Date();

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    let startDateTimeStamp = new Date().setFullYear(new Date().getFullYear() - 10);
    this.unavailabilityForm = this.formBuilder.group({
      startDate: [new Date(startDateTimeStamp)],
      endDate: [new Date()]
    });
  }
}

It will resolve your concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM