Calculate Age in Years AND Months
Hello, I need a script to calculate someone's age in years and months (e.g., 14 years and 5 months old).
I have searched way too long and have been able to get years to caculate with no problem (using the Date of Birth and current date), but cannot for the life of me figure out the months part.
I'm making a form so I can use either the current date or the date the form was completed (input by the user). I would prefer to use the date the form was completed but will use either.
I also don't care either way if this is in one field or two (one for years and one for months). Right now I have two separarte fields set up since I was only able to get the years to calculate. Can someone please help? Please?
Here is the script I found after searching and am using to calcualte years:
event.value = "";
dobValue = getField("DOB").value;
if (dobValue!="") {
dob = util.scand("mm.dd.yyyy", dobValue);
now = new Date();
age = now.getFullYear() - dob.getFullYear();
if (now.getMonth() < dob.getMonth())
age--;
else if (now.getMonth()==dob.getMonth() && now.getDate() < dob.getDate())
age--;
event.value = age;
}
var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("m/dd/yyyy", dobValue);
var today = new Date();
var age = today.getTime() - dob.getTime();
var nAgeMilliseconds = today.getTime() - dob.getTime();
var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425));
event.value = Math.floor(nAgeYears);
var nMonthsLeft = Math.floor((nAgeYears - event.value) * 12);
}
else
$.rawValue = null
endif
