Angular.js function to calculate age

Asked

Viewed 1,282 times

3

Good evening, everyone.

In my comic book, I store customers' date of birth.

I have a list that presents:

name | Cpf | age?

How could I take the data stored in the bd and using Angular.js or php calculate the current age and fill in my list?

In the case, the data are presented in the list are thus:

{{data.name}} - {data.Cpf}} - age????

Although I knew very little about Angular.js, I accepted the challenge of studying and modifying some systems I used to do with Jquery. So far I’m enjoying it.

  • You can display the data "Name, Cpf, Age" by doing a query by default?

  • Can I get @Vanderson ... I think I’ll even guess what you’re going to suggest... calculate by query? rsrsrsrsrsr I’ll try that... Tks

2 answers

6

You can implement a function:

  • Controller
$scope.calcularIdade = function calcularIdade(nascimento) {
    // Obtém a idade em milissegundos
    var idadeDifMs = Date.now() - nascimento.getTime();

    // Converte os milissegundos em data e subtrai da era linux
    var idadeData = new Date(idadeDifMs);
    var idade = idadeData.getUTCFullYear() - 1970;

    return idade;
}
  • HTML

    {{ calcularIdade(data.nascimento) }}
    

Note that this code has precision problems. The margin of error may be a few hours in a few years, or during daylight saving time. It is recommended to use a library to do this reliably.

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.