jquery calculant l’âge en fonction de la date de naissance

J’ai un textfield où l’utilisateur entre la date de naissance au mm/dd/yyyy . Je souhaite remplir automatiquement le champ d’ age de l’utilisateur en fonction de sa dob entrée.

le champ de textfield age doit être renseigné lorsque l’utilisateur quitte le champ dob.

Quel est le moyen jQuery de faire cela?

Voir Calculer l’âge en JavaScript pour une fonction permettant de calculer l’âge qui gère les années bissextiles de manière appropriée.

Vous pouvez ensuite utiliser cette fonction pour calculer l’âge de l’utilisateur, comme suit:

 var age = getAge(new Date($("#date").value())); 

.. puis définissez le champ d’age sur ce nombre:

 $("#date").bind("blur", function() { $("#age").value(age) }); 
 function (row) { var age = 0; var birthday = row["FechaNacimiento"]; age = new Date().getFullYear() - birthday.getFullYear(); if (new Date() < birthday.setFullYear(birthday.getFullYear() + age)) { age--; } return age; } 

tard mais ça marche ...

Essayez le plugin jquery.dates

 var d = $("#mytextboxID").dateDiff("unit", date); 

Ici, nous le testons en prenant les dates de début et de fin. Vous pouvez également l’utiliser pour calculer l’âge en mois et en jours.

 var end = new Date(2015, 09, 02, 00, 0, 0, 0), begin = new Date(2015, 09, 1, 00, 0, 0, 0), e = new Date(end), b = new Date(begin), bMonth = b.getMonth(), bYear = b.getFullYear(), eYear = e.getFullYear(), eMonth = e.getMonth(), bDay = b.getDate(), eDay = e.getDate() + 1; if ((eMonth == 0) || (eMonth == 2) || (eMonth == 4) || (eMonth == 6) || (eMonth == 7) || (eMonth == 9) || (eMonth == 11)) { var eDays = 31; } if ((eMonth == 3) || (eMonth == 5) || (eMonth == 8) || (eMonth == 10)) { var eDays = 30; } if (eMonth == 1 && ((eYear % 4 == 0) && (eYear % 100 != 0)) || (eYear % 400 == 0)) { var eDays = 29; } if (eMonth == 1 && ((eYear % 4 != 0) || (eYear % 100 == 0))) { var eDays = 28; } if ((bMonth == 0) || (bMonth == 2) || (bMonth == 4) || (bMonth == 6) || (bMonth == 7) || (bMonth == 9) || (bMonth == 11)) { var bDays = 31; } if ((bMonth == 3) || (bMonth == 5) || (bMonth == 8) || (bMonth == 10)) { var bDays = 30; } if (bMonth == 1 && ((bYear % 4 == 0) && (bYear % 100 != 0)) || (bYear % 400 == 0)) { var bDays = 29; } if (bMonth == 1 && ((bYear % 4 != 0) || (bYear % 100 == 0))) { var bDays = 28; } var FirstMonthDiff = bDays - bDay + 1; if (eDay - bDay < 0) { eMonth = eMonth - 1; eDay = eDay + eDays; } var daysDiff = eDay - bDay; if (eMonth - bMonth < 0) { eYear = eYear - 1; eMonth = eMonth + 12; } var monthDiff = eMonth - bMonth, yearDiff = eYear - bYear; if (daysDiff == eDays) { daysDiff = 0; monthDiff = monthDiff + 1; if (monthDiff == 12) { monthDiff = 0; yearDiff = yearDiff + 1; } } if ((FirstMonthDiff != bDays) && (eDay - 1 == eDays)) { daysDiff = FirstMonthDiff; } console.log(yearDiff + " Year(s)" + " " + monthDiff + " month(s) " + daysDiff + " days(s)"); 

Je suis venu avec une solution simple pour cela. C’est une fonction de change jQuery qui envoie la valeur à un champ d’âge en entrée.

 $(document).ready(function() { $('#dob input').change(function() { var today = new Date(); var dd = Number(today.getDate()); var mm = Number(today.getMonth() + 1); var yyyy = Number(today.getFullYear()); var myBD = $('#dob input').val(); var myBDM = Number(myBD.split("/")[0]) var myBDD = Number(myBD.split("/")[1]) var myBDY = Number(myBD.split("/")[2]) var age = yyyy - myBDY; if (mm < myBDM) { age = age - 1; } else if (mm == myBDM && dd < myBDD) { age = age - 1; } $('#age input').val(age); }); }); 

Sur flou de votre zone de texte, vous devrez calculer la différence de date entre ce qu’ils ont entré et now() .

 $('#birthdate').blur(function() { $("#ageTextBox").val(getAge(parseDate($('#birthdate').val()))); }); function getAge(birthDate) { var now = new Date(); function isLeap(year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } // days since the birthdate var days = Math.floor((now.getTime() - birthDate.getTime()) / 1000 / 60 / 60 / 24); var age = 0; // iterate the years for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++) { var daysInYear = isLeap(y) ? 366 : 365; if (days >= daysInYear) { days -= daysInYear; age++; // increment the age only if there are available enough days for the year. } } return age; }