-2
I need help creating a Javascript code or using jQuery, which should read the value of the person’s date of birth (I’m using datepicker) and calculate how old they are. I have to show in div speaking if she is major or not, I have following code:
$("#entraridade").click(function () {
var idade = 2016 - $("#date").val();
$("#mostrar").text("Ola " + $("#name").val());
$("#mostrar2").text("sua idade e :" + $("#idade").val());
});
$("#datepicker").datepicker({
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'dd-mm-yy',
dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S', 'D'],
dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb', 'Dom'],
monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
nextText: 'Proximo',
prevText: 'Anterior'
});
<body>
<form id="form">
Nome:
<input type="text" id="name"><br>
Data de nascimento:
<input type="text" id="datepicker" name="data"/><br>
<input type="button" id="entraridade" value="Ok">
</form>
<div id="resultado"></div>
<div id="resultado2"></div>
</body>
first vc defines what is the age of majority (in case 18 years), then it is only subtract year (current - ano_birth) if the result is less than the age of majority (18), it is smaller, if not greater.
– Ivan Ferrer