Age From Birthday Calculator
Figured this might be handy to some folks… Just enter in a birthdate (or just any date for that matter, doesn’t necessarily have to be a birthday), and it will give you the number of years that have passed since then! Why use your head when we have computers!?
Month:
Day:
Year:
(YYYY)
UNIMPORTANT NOTE: This script uses the average number of days in a year as part of its calculation. If I hear about that causing any problems I’ll look into another way of doing it, but I think it’s the most reasonable, far more so than using a whole number like 365.
The JavaScript:
function getAgeFromBday() {
var month = document.getElementById('bdc-month').value;
var day = document.getElementById('bdc-day').value;
var year = document.getElementById('bdc-year').value;
var bdate = new Date(year, month, day);
if (bdate.getDate() != day || bdate.getMonth() != month || bdate.getFullYear() != year) {
alert('That date appears to be invalid!');
return false;
}
var today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
if (bdate > today) {
alert('Provided date must fall before today\'s date!');
return false;
}
alert(Math.floor((today - bdate) / 31556952000));
}
The markup:
<p>
<strong>Month:</strong><br />
<select name="bdc-month" id="bdc-month">
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
</p>
<p>
<strong>Day:</strong><br />
<input name="bdc-day" id="bdc-day" style="width: 15px;" type="text" />
</p>
<p>
<strong>Year:</strong><br />
<input name="bdc-year" id="bdc-year" style="width: 30px;" type="text" /> (YYYY)
</p>
<p>
<button type="submit" onclick="getAgeFromBday()"><strong>DO IT</strong></button>
</p>