1

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 ???

7

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you bro for your vote . but still i have that problem . i can select the date between 2011 and 2020 i want can not select between to date
Ohh I am not getting the question properly give me 5 mins I will solve your problem
Sorry @Mr-Programer your problem is not fixed it's not possible. because of we are not set the max date below current date.
You need to use two dropdown
Hello @Mr-Programer I have changes my answer. You can take two different date pickers and then your concerns is resolved
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.