hi, I've got a problem and i really need help. this is my code <script> var y= new Date(); y.getFullYear() + '<br />'; alert(y); var year = new Date(); year.setFullYear(prompt('Enter the year','1990'),prompt('Enter the month','1'), prompt('Enter the day','1')); alert(year); var yy = y-year; alert(yy); </script> i want when a user write his/her birthday JS calculate the age. that's all
There's bound to be dozens of example scripts for this on the web. Try searching on "javascript age calculator" and I'm sure you'll find one.
Those y, year and yy are date objects. You need to convert them to numbers - via .getFullYear() - to be able to perform some math. var y= new Date(); var year = new Date(); year.setFullYear( prompt('Enter the year',1990), prompt('Enter the month',1), prompt('Enter the day',1)); var yy = y.getFullYear() - year.getFullYear(); alert(yy); Code (markup):
this might help.. the Ultimate Age Calculator: http://www.javascriptkit.com/script/script2/calculateage.shtml you can get an idea from there and create your own