0

This is my HTML code, `

        <mat-form-field class="datepicker">
            <mat-label>Pick a date</mat-label>
            <input matInput 
                    #resultPickerModel="ngModel" 
                    [matDatepicker]="resultPicker" 
                    [(ngModel)]="date"
                    [min]="minDate" 
                    [max]="maxDate"
                     (input)="ageFromDateOfBirthday($event)">
            <mat-datepicker-toggle matSuffix [for]="resultPicker"></mat-datepicker-toggle>
            <mat-datepicker #resultPicker>
            </mat-datepicker>
            <mat-error *ngIf="resultPickerModel.hasError('matDatepickerParse')">
                "{{resultPickerModel.getError('matDatepickerParse').text}}"
                is not a valid date!
            </mat-error>
            <mat-error *ngIf="resultPickerModel.hasError('matDatepickerMin')">
                Minimum date should be {{minDate | date }}</mat-error>
            <mat-error *ngIf="resultPickerModel.hasError('matDatepickerMax')">
                Maximum date should be {{maxDate | date }}
            </mat-error>
        </mat-form-field>
    </mat-grid-tile>
    <mat-grid-tile>
        <mat-form-field appearance="fill" class="age">
            <mat-label>Age</mat-label>
            <input type="number" matInput >
        </mat-form-field>
    </mat-grid-tile>`

Here, I have to provide the AGE in input textbox when we select age in date picker. Kindly let me know any solutions for this.

Dhanush
  • 1
  • 3
  • Do you mean "when we select **date of birth** in date picker"? Possible duplicate: https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd. – Oliver Feb 19 '22 at 16:32
  • 1
    Does this answer your question? [Calculate age given the birth date in the format YYYYMMDD](https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd) – Oliver Feb 19 '22 at 16:32

1 Answers1

0

Maybe this code could help you:

getAge(birthDate: Date): number {
    const ageTilNowInMilliseconds = Date.now() - birthDate.getTime();
    const ageDate = new Date(ageTilNowInMilliseconds);
    return Math.abs(ageDate.getUTCFullYear() - 1970); // Because computers count the today date from the 1st of January 1970
}

I hope it helped.

Bryan BERGER
  • 116
  • 9