I want to calculate the age of someone on race day based on their Date of Birth.

Three text element fields

1. dob

2. Race Date (racedate) (read only)

3. Age (age) (on race day based on their dob)

Place this script in the initialization of the Form via More...

function ff_NAME OF FORM_init()
{setInterval(calAge, 500);
} // ff_NAME OF FORM_init

function calAge() {
var dateBirth = ff_getElementByName('dob').value;
var dobParts = dateBirth.split("/");
var dob = new Date(dobParts[2], (dobParts[1] - 1), dobParts[0]);

var dobYear = dob.getFullYear();
var dobMonth = dob.getMonth();
var dobDay = dob.getDate()

var secondDate = ff_getElementByName('racedate').value;
var dateParts = secondDate.split("/");
var date2 = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

var dateYear = date2.getFullYear();
var dateMonth = date2.getMonth();
var dateDay = date2.getDate()


var age = dateYear - dobYear;
var ageMonth = dateMonth - dobMonth;
var ageDay = dateDay - dobDay;

if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)) {
age = parseInt(age) - 1;
}

ff_getElementByName('age').value = age;
}

WE LOVE JOOMLA

Joomwalker is not affiliated with or endorsed by the Joomla Project or Open Source Matters.
The Joomla name and logo are used under a limited license granted by Open Source Matters the trademark holder in the United States and other countries.