Techdive
Techdive

Reputation: 1043

How to make yesterday's date as the date to appear in mat-datepicker - angular material

I want to update and make yesterday's date appear as the date in mat-datepicker.

I tried to implement it the following way but it was not successful. My code -

app.component.html

<mat-form-field class="example-full-width" [floatLabel]="hideLabel">
                <input matInput [formControl]="onlineDate" name="onlineDate" [max]="currentDate"  
                  [matDatepicker]="picker" required placeholder="Online Date "
                  autocomplete="off" />
                <mat-datepicker-toggle matSuffix [for]="picker">
                </mat-datepicker-toggle>
                <mat-datepicker #picker></mat-datepicker>
                
              </mat-form-field> 

app.component.ts

export class ApplicantComponent implements OnInit, AfterViewInit {
  onlineDate: FormControl;
  currentDate = new Date();
  today = new Date()
  yesterday = new Date();


 constructor(
       this.onlineDate = new FormControl(this.yesterday.setDate(this.yesterday.getDate() - 1))
  }

}

With the above code, I am unable to get any date in display. Screenshot for reference-

enter image description here

Upvotes: -1

Views: 490

Answers (1)

Techdive
Techdive

Reputation: 1043

I solved my issue by putting -

 constructor(
       this.onlineDate = new FormControl(new Date(new Date().setDate(new Date().getDate() + 1)));

  }

Upvotes: 0

Related Questions