2 Replies Latest reply on Oct 17, 2012 7:56 AM by aglittleleague

# Calculate age to a specific date

I have no experience with Javascript so I'm hoping that you can help me. We have a Little League player registration form that needs to evaluate the player's age as of April 30 for the current season. So I have two fields, one is the season (e.g., 2013) and the other field is the player's date of birth.

You can see the form here.

TIA!

- Dave

• ###### 1. Re: Calculate age to a specific date

Yes you can compute the age, but you need to write a custom JavaScript calculation to perform the calculation.

How are fractional years handled?

// get date of birth string
var cDob = this.getField("DOB").value;
// get the season year string
var cSeason = this.getField("Season").valueAsString;
// compute only if we have a DOB and Season Year
if(cDob != "" && cSeason != "") {
// convert dob string to a date object
var oDob = util.scand("mm/dd/yyyy", cDob);
// convert dob to milliseconds since the Epoch date (Jan 1, 1970)
var nDob = oDob.getTime();
// get milliseconds sinec Epoch date for April 30, 2013
oCutoff = util.scand("mm/dd/yyyy", "04/30/" + cSeason)
nCutoff = oCutoff.getTime();

// compute difference in years
event.value = Math.floor(((nCutoff  - nDob) / (1000 * 60 * 60 * 24 * 365.2425)) + 0.0007);
} // end non-blank DOB

• ###### 2. Re: Calculate age to a specific date

That solved it perfectly. Thanks!