Calculate age from a date

1

How can I calculate the age from a date?

This is my code, when I run it I get undefined in the field that I want the age to appear.

<div class="row">
  <div class="small-6 columns">
    <label>Edad</label>
  </div>
  <div class="small-2 columns">
    <input readonly type="text" [value]="CalculateAge()">
  </div>
</div>
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;
}
    
asked by EDWARD FABIAN TAPIERO GOMEZ 02.06.2017 в 20:53
source

3 answers

0

I think the problem is that your CalculateAge() function does not return the age value to be binded to input

A possible correction would be

CalculateAge(): number {
    if (this.datosDTO.datosPersonales.fechaNacimiento) {
        var timeDiff = Math.abs(Date.now() - this.datosDTO.datosPersonales.fechaNacimiento);
        return Math.ceil((timeDiff / (1000 * 3600 * 24)) / 365);
    } else {
        return null;
    }
}
    
answered by 07.06.2017 в 21:07
0

You can take your PersonData class and create a calculated variable (a function that is used as a variable) that makes that calculation.

export class DatosPersona {

  tipoIdentificacion: string = "";
  numeroIdentificacion: string = "";
  nombres: string = "";
  apellidos: string = "";
  fechaNacimiento: number;
  get edad(): number {
    return ~~((Date.now() - this.fechaNacimiento || Date.now()) / (24 * 3600 * 365.25 * 1000));
  }

}

Then you can use it like this:

this.datosDTO.datosPersonales.edad
    
answered by 29.12.2017 в 02:05
0
  • Make sure that the function returns a data of type number instead of void .
  • Instead of var use let or const .
  •   

    I added a else to return a 0 , so I make sure that I always function I returned a number.

    CalculateAge(): number {
        if (this.datosDTO.datosPersonales.fechaNacimiento) {
            let timeDiff = Math.abs(Date.now() - <any>this.datosDTO.datosPersonales.fechaNacimiento);
            return Math.ceil((timeDiff / (1000 * 3600 * 24)) / 365);
        } else {
            return 0;
        }
    }
    
      

    This is my proposal which works with Date objects and I use operations that I can perform with dates. I also use a getter function that is responsible for returning a data of numeric type.

    get CalculateAge(): number {
        const today: Date = new Date();
        const birthDate: Date = new Date(this.datosDTO.datosPersonales.fechaNacimiento);
        let age: number = today.getFullYear() - birthDate.getFullYear();
        const month: number = today.getMonth() - birthDate.getMonth();
        if (month < 0 || (month === 0 && today.getDate() < birthDate.getDate())) {
            age--;
        }
        return age;
    }
    
        
    answered by 01.10.2018 в 00:11