javascript - Jquery get age by input value -
how calculate age of user, depending on date entered in textbox?
i want able calculate exact age of yyyy/mm/dd, far i've managed year.
the code i've tried far:
function onclick() { var txtdate = $("#txtdate").val(); var date1 = new date(); date1.setfullyear(txtdate); var d1 = date1.getfullyear(); var date2 = new date(); var d2 = date2.getfullyear(); var age = d2 - d1; document.getelementbyid("diven").innerhtml = age;
}
any ideas?
when set date using this: new date(birthyear, birthmonth, birthday);
need subtract 1
month. months counted 00
.
for example,
var testdate = new date(1988,10,12) sat nov 12 1988 00:00:00
you can try alternate way:
var today = new date(); var inputbirthdate= new date(birthyear, birthmonth - 1, birthday); var age = today.getfullyear() - inputbirthdate.getfullyear(); var month = today.getmonth() - inputbirthdate.getmonth(); if (month < 0 || (month === 0 && today.getdate() < inputbirthdate.getdate())) { age--; } console.log(age);
this return correct age based on input birth date.
Comments
Post a Comment