Can anyone assist me with calculating age from a given date in Angular? I have written the following code but I keep getting undefined in the expected field.
This is Component 1
import {
Component,
OnInit,
Input
} from '@angular/core';
import {
DatosDTO
} from '../../dto/datos'
@Component({
selector: 'app-datos-personales',
templateUrl: './datos-personales.component.html',
styleUrls: ['./datos-personales.component.css']
})
export class DatosPersonalesComponent implements OnInit {
@Input()
datosDTO: DatosDTO;
public age: number;
constructor() {}
ngOnInit() {
this.datosDTO = new DatosDTO();
}
CalculateAge(): void {
if (this.datosDTO.datosPersonales.fechaNacimiento) {
var timeDiff = Math.abs(Date.now() - this.datosDTO.datosPersonales.fechaNacimiento);
this.age = Math.ceil((timeDiff / (1000 * 3600 * 24)) / 365);
}
}
}
export class DatosPersona {
tipoIdentificacion: string = "";
numeroIdentificacion: string = "";
nombres: string = "";
apellidos: string = "";
fechaNacimiento: number;
}
This is the HTML template
<div class="row">
<div class="small-6 columns">
<label>Age</label>
</div>
<div class="small-2 columns">
<input readonly type="text" [value]="CalculateAge()">
</div>
</div>
Your assistance would be greatly appreciated.