Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do we set a datepicker default value to 1 year from today?

How do we set to an angular mat date picker where the default selected value is 1 year from today ?. Thanks.

When the date picket loads the default value should be 1 year from now.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#html code

<mat-form-field appearance="fill" class="full-width"  [ngClass]="{'date-field-error-underline': walgreensFields.formError.nextNoticeEndDate !== null}">
                <mat-label [ngClass]="{'c-error':walgreensFields.formError.nextNoticeEndDate !== null}">Expiration Date Range (End)</mat-label>
                  <input 
                  matInput 
                  hidden
                  (dateChange)="setNextNoticeEndDate($event)" 
                  [(ngModel)]="walgreensFields.nextNoticeEndDate"
                  [matDatepicker]="endDatePicker"
                  >
                <mat-datepicker-toggle matSuffix [for]="endDatePicker"></mat-datepicker-toggle>
                <mat-datepicker #endDatePicker></mat-datepicker>
                <div class="mat-form-field-subscript-wrapper date-field-custom-error" *ngIf="walgreensFields.formError.nextNoticeEndDate !== null">
                  <div class="mat-error">{{walgreensFields.formError.nextNoticeEndDate}}</div>                        
                </div>
             </mat-form-field>

>Solution :

You can set the default value of your walgreensFields.nextNoticeEndDate that is binded to the datepicker using [(ngModel)] to one year from today’s date. To do this you could use Date#setFullYear to set the year to one more than current Date#getFullYear:


  exampleDate = new Date();

  ngOnInit(): void {
     walgreensFields.nextNoticeEndDate = this.exampleDate.setFullYear(this.exampleDate.getFullYear() + 1);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading