0

I have a requirement to show the dates for start date and end date i.e., if the start date is dd/mm/yyyy format 10/09/2020 and end date should be till yesterday i.e., 09/09/2020 and all the remaining dates should be disabled.

How can I achieve this?

 <mat-form-field color="accent" appearance="fill">
              <mat-label>Start Date</mat-label>
              <input matInput [matDatepicker]="picker1"  [max]="tomorrow" [formControl]="startDate">
              <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
              <mat-datepicker #picker1></mat-datepicker>
            </mat-form-field>

          <mat-form-field color="accent" appearance="fill">
            <mat-label>End Date</mat-label>
            <input matInput [matDatepicker]="picker2" [min]="today" max="tomorrow" [formControl]="endDate">
            <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
            <mat-datepicker #picker2></mat-datepicker>
          </mat-form-field>
3
  • You can also, if only want a min and max value, use [min] and [max] property: material.angular.io/components/datepicker/… Commented Sep 10, 2020 at 7:20
  • @Eliseo, i tried the same @Input() min: any; @Input() max: any; tomorrow = new Date(); future = new Date(); this.tomorrow.setDate(this.tomorrow.getDate()); this.future.setDate(this.future.getDate()); Commented Sep 10, 2020 at 8:13
  • Sorry, I've explained it very bad, check my answer Commented Sep 10, 2020 at 10:27

2 Answers 2

1

You can use filter validation.

<mat-form-field class="example-full-width" appearance="fill">
  <mat-label>Choose a date</mat-label>
  <input matInput [matDatepickerFilter]="myFilter" [matDatepicker]="picker">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

import {Component} from '@angular/core';

/** @title Datepicker with filter validation */
@Component({
  selector: 'datepicker-filter-example',
  templateUrl: 'datepicker-filter-example.html',
})
export class DatepickerFilterExample {
  myFilter = (d: Date | null): boolean => {
    const day = (d || new Date()).getDay();
    // Prevent Saturday and Sunday from being selected.
    return day !== 0 && day !== 6;
  }
}

Example from the offical docs here

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

Comments

0

May I want to say. In .ts

today=new Date();
tomorrow=new Date(this.today.getTime()+24*60*60*1000);

in .html

<input matInput [matDatepicker]="picker" [min]="today" [max]="tomorrow">

2 Comments

i have added the HTML part of what i tried but it's still not working..
remember that mat-date-picker works with javascript Date object. Just put the answer in a stackblitz:stackblitz.com/edit/angular-tt1cdq?file=src/app/…. Check if your error is in another place

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.