function Angular.js to calculate age

3

Good evening guys.

In my bd, I store the date of birth of clients.

I have a list that presents:

name | cpf | age?

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

In this case, the data that appears in the list looks like this:

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

Although I know very little about Angular.js, I accepted the challenge of studying and modifying some systems that I used to do with JQuery. So far I'm enjoying it.

    
asked by anonymous 18.05.2015 / 23:48

2 answers

1

Solved, using a direct calculation in the query. So I did not even have to create some JavaScript function ... it was fast and practical!

SELECT 
  DATE_FORMAT(NOW(),'%Y') - 
  DATE_FORMAT(data_nascimento,'%Y') -
  (DATE_FORMAT(NOW(),'00-%m-%d') < DATE_FORMAT(data_nascimento,'00-%m-%'))
AS idade;

Retrieved from link

    
19.05.2015 / 01:02
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 can be a few hours in a few years, or during daylight saving time. It is advisable to use a library to do this reliably.

    
19.05.2015 / 00:15