Below codes divided into two parts, one part in head tag and another tag in body.
<html>
<head>
<!– AGE CALCULATOR –>
<script language=”JavaScript” type=”text/javascript”>
<!–
function getAge() {
month = (document.form1.month.value – 1);
date = document.form1.date.value;
year = document.form1.year.value;
if (month != parseInt(month)) { alert(‘Type Month of birth in digits only!’); return false; }
if (date != parseInt(date)) { alert(‘Type Date of birth in digits only!’); return false; }
if (year != parseInt(year)) { alert(‘Type Year of birth in digits only!’); return false; }
if (year.length < 4) { alert(‘Type Year of birth in full!’); return false; }
today = new Date();
dateStr = today.getDate();
monthStr = today.getMonth();
yearStr = today.getFullYear();
theYear = yearStr – year;
theMonth = monthStr – month;
theDate = dateStr – date;
var days = “”;
if (monthStr == 0 || monthStr == 2 || monthStr == 4 || monthStr == 6 || monthStr == 7 || monthStr == 9 || monthStr == 11) days = 31;
if (monthStr == 3 || monthStr == 5 || monthStr == 8 || monthStr == 10) days = 30;
if (monthStr == 1) days = 28;
document.form1.inYears.value = theYear;
if (month < monthStr && date > dateStr) { document.form1.inYears.value = parseInt(document.form1.inYears.value) + 1;
document.form1.inMonths.value = theMonth – 1; }
if (month < monthStr && date <= dateStr) { document.form1.inMonths.value = theMonth; }
else if (month == monthStr && (date < dateStr || date == dateStr)) { document.form1.inMonths.value = 0; }
else if (month == monthStr && date > dateStr) { document.form1.inMonths.value = 11; }
else if (month > monthStr && date <= dateStr) { document.form1.inYears.value = document.form1.inYears.value – 1;
document.form1.inMonths.value = ((12 – -(theMonth)) + 1); }
else if (month > monthStr && date > dateStr) { document.form1.inMonths.value = ((12 – -(theMonth))); }
if (date < dateStr) { document.form1.inDays.value = theDate; }
else if (date == dateStr) { document.form1.inDays.value = 0; }
else { document.form1.inYears.value = document.form1.inYears.value – 1; document.form1.inDays.value = days – (-(theDate)); }
}
// –>
</script>
</head>
<body>
<form name=”form1″>
<table align=”center” width=”380″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td align=”center”>
MM: <input type=”text” maxLength=”2″ size=”2″ name=”month” value=”01″ />
DD: <input type=”text” maxLength=”2″ size=”2″ name=”date” value=”30″ />
YYYY: <input type=”text” maxLength=”4″ size=”4″ name=”year” value=”1990″ />
<input type=”button” value=”Get Age” onClick=”getAge()” /><br /><br />
</td>
</tr>
<tr>
<td align=”center”>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”120″>
<tr><td align=”right”><input type=”text” size=”2″ name=”inYears” value=”” /></td><td align=”left”>Years</td></tr>
<tr><td align=”right”><input type=”text” size=”2″ name=”inMonths” value=”” /></td><td align=”left”>Months</td></tr>
<tr><td align=”right”><input type=”text” size=”2″ name=”inDays” value=”” /></td><td align=”left”>Days</td></tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
Click here to download source code Age calculator
javascript should be little more conservative
Thanks for your comments, I will try to find a time to do it for you.
Rana
I’m learning javascript. Here’s my simple code to display my age on a webpage. Just change the numbers to your birthdate!
var todaysDate = new Date();
var y = todaysDate.getFullYear()
var m = todaysDate.getMonth() + 1
var d = todaysDate.getDate() + 1
var myAge = (y-1971);
if (m<=1 && d<9)
{
myAge = myAge – 1;
}
document.write(“Age: ” + myAge);
This does not really compute the exact age. Nice try though.
I could not get it to work on my site.
Thanks,
Karen
Great goods from you, man. I’ve understand your stuff previous to and you’re just too great. I actually like what you’ve acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can not wait to read much more from you. This is actually a tremendous site.
Good! keep working on.
thanks
I enjoy what you guys are usually up too. This type of clever work and reporting!
Keep up the excellent works guys I’ve included you guys to my blogroll.
Plz Age calulator code
Plz Age calulator code IN HTML