I need assistance in calculating the age from a date input in an existing form using HTML5 and Angular. I want to achieve this without requiring the user to click anything.
Although I am able to retrieve the date, the format is causing issues with my code. Is there a way to modify the format or explore a more efficient alternative solution?
Any guidance or support on this matter would be greatly appreciated.
HTML:
<input type="date" name="dob" [(ngModel)]="birthdate">
<button (click)="getAge()">Get Age</button>
<input type="text" name="age" [(ngModel)]="data.age">
TS:
data: any;
birthdate: any;
constructor() {
this.data = {};
this.data.age = '';
this.birthdate = this.birthdate;
}
public getAge() {
console.log(this.birthdate);
const timeDiff = Math.abs(Date.now() - this.birthdate);
console.log(timeDiff);
this.data.age = Math.floor((timeDiff / (1000 * 3600 * 24)) / 365);
console.log(this.data.age);
}