Using the date.js library, I created a function to get someones age when supplied a birthdate and the date at which you require the age. What do ye think? (I have checked that the second Date is after the first date in the function that calls this one).
/**
* A function which will calculate the age in years of the first Date object, dateOfBirthObject,
* at the Date of the second Date Object, ageDateObject.
* It will use the functions available in date.js.
* @ param Date date of birth
* @ param Date date at which age needed
* @ return years age
*/
function getAge(dateOfBirthObject, ageDateObject) {
// A variable which will store the current year
var yearToday = ageDateObject.toString('yyyy');
// A variable which will be used to store the birth year
var yearBirthDate = dateOfBirthObject.toString('yyyy');
// A variable to store the amount of years between the birth year and this year
var yearsBetween = yearToday - yearBirthDate;
// A variable which will be used to count the leap years, or more specifically,
// how many times the 29th February has occured during the two periods
var leapYears = 0;
// We loop through all the years from the birth year to the current year
for (var i = yearBirthDate; i <= yearToday; i++) {
// We evaluate if the current year is a leap year
if (Date.isLeapYear(i)) {
// We evaluate if the current year is the birth year
if (i == yearBirthDate) {
// We must evaluate if the birth date occured on or after the 29th February
// We create a date String of 29th February of the birth year
var dOBFeb29 = new String('29/02/' + yearBirthDate);
// We create a date object, will return null if the String date is not valid
var dOBFeb29DateObject = Date.parseExact(dOBFeb29, 'dd/MM/yyyy');
// We check if the date of birth is on (0), before (-1), or after (1) the 29th February
var dobAfter = dateOfBirthObject.compareTo(dOBFeb29DateObject);
// If date of birth is after the 29th February, we do nothing, else we increment the variable leapYears
if (dobAfter == 1) {}
else {
leapYears++;
}
}
// We evaluate it the current year is this year
else if (i == yearToday) {
// We must evaluate if the 29th February has occured this year
// We create a date String of 29th February of the current year
var thisYearFeb29 = new String('29/02/' + yearToday);
// We create a date object, will return null if the String date is not valid
var thisYearFeb29DateObject = Date.parseExact(thisYearFeb29, 'dd/MM/yyyy');
// We check if todays date is on (0), before (-1), or after (1) the 29th February
var thisYearAfter = ageDateObject.compareTo(thisYearFeb29DateObject);
// If todays date is before the 29th February, we do nothing, else we increment the variable leapYears
if (dobAfter == -1) {}
else {
leapYears++;
}
}
// For all other years we increment the variable leapYears
else {
leapYears++;
}
}
}
// We get the age in milliseconds and then work up to a value in years
var miliseconds = ageDateObject - dateOfBirthObject;
var seconds = miliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var decimalPart;
var yearDivisor;
var years;
// We need to set the decimal portion of the yearDivisor
if (yearsBetween == 0) {
decimalPart = 0;
}
else {
decimalPart = leapYears / yearsBetween;
}
// We add the decimal part to 365
yearDivisor = 365 + decimalPart;
// We calculate the age in years
years = days / yearDivisor;
return years;
}