Home > database >  how set min date for a datepicker
how set min date for a datepicker

Time:04-28

How do we set the min date for a calendar , the min is 2 days from the current date. so if today is 27 then the current or min date should be 29 since 2 days from now it is 29. Any idea guys ? Thanks.

enter image description here

#html code

<mat-form-field >
  <input matInput #input="ngModel" [(ngModel)]="date" [min]="minDate" [max]="maxDate" [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>

  <mat-error *ngIf="input.hasError('matDatepickerMax')">Date should be inferior</mat-error>
  
</mat-form-field>

#ts code

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

/** @title Datepicker with min & max validation */
@Component({
  selector: 'datepicker-min-max-example',
  templateUrl: 'datepicker-min-max-example.html',
  styleUrls: ['datepicker-min-max-example.css'],
})
export class DatepickerMinMaxExample {
    
  minDate = new Date();
}

CodePudding user response:

Try:

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

/** @title Datepicker with min & max validation */
@Component({
  selector: 'datepicker-min-max-example',
  templateUrl: 'datepicker-min-max-example.html',
  styleUrls: ['datepicker-min-max-example.css'],
})
export class DatepickerMinMaxExample {
    
  minDate = (new Date()).setDate(new Date().getDate()   2);
}
  • Related