Last active
February 3, 2022 12:06
-
-
Save dantetesta/2c1e6b55b959300b8a804ea780e12658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
jQuery( document ).ready(function() { | |
var datanascimento = jQuery('#nascimento h2').text(); | |
var idade = calculaIdade(datanascimento); | |
jQuery('#idade h2').text(idade + ' Anos'); | |
//NÃO MEXE AQUI | |
function calculaIdade(dataNasc){ | |
var dataAtual = new Date(); | |
var anoAtual = dataAtual.getFullYear(); | |
var anoNascParts = dataNasc.split('/'); | |
var diaNasc =anoNascParts[0]; | |
var mesNasc =anoNascParts[1]; | |
var anoNasc =anoNascParts[2]; | |
var idade = anoAtual - anoNasc; | |
var mesAtual = dataAtual.getMonth() + 1; | |
//Se mes atual for menor que o nascimento, nao fez aniversario ainda; | |
if(mesAtual < mesNasc){ | |
idade--; | |
} else { | |
//Se estiver no mes do nascimento, verificar o dia | |
if(mesAtual == mesNasc){ | |
if(new Date().getDate() < diaNasc ){ | |
//Se a data atual for menor que o dia de nascimento ele ainda nao fez aniversario | |
idade--; | |
} | |
} | |
} | |
return idade; | |
} | |
}); | |
//FONTE: http://blog.dbins.com.br/calculando-a-idade-com-javascript | |
</script> | |
//############ CRIE AS ENTRADAS DOS DADOS NOS ID CSS | |
#nascimento | |
#idade | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment