Entry
How to calculate an age exactly from the given date of birth
Mar 5th, 2000 20:28
Hiroto Sekine, https://buttons.ihug.co.nz/all/enrolmentE.htm
Here is a sample source code to get age exactly. This function is
useful for such an enrokment form, members application form which
requires user's age. You can try this function using your birthday at
above site. Please mind that the implemented source code is alittle bit
different from the code below.
function makeDaysOfMonth(){
var i = 0;
this[i++] = 0; // dummy
this[i++] = 31;
this[i++] = 29;
this[i++] = 31;
this[i++] = 30;
this[i++] = 31;
this[i++] = 30;
this[i++] = 31;
this[i++] = 31;
this[i++] = 30;
this[i++] = 31;
this[i++] = 30;
this[i ] = 31;
this.length = i;
}
function calcAge(dd, mm, yy){
var t, mon, day, year, DD, MM, YY, age;
var MTB = new makeDaysOfMonth();
YY = parseInt(yy); // year of birth (4 digits)
MM = parseInt(mm); // month of birth (1-12)
DD = parseInt(dd); // date of birth (1-31)
if (MTB[MM] < DD || DD < 1) return -1;
t = new Date(); // get current date
year = t.getFullYear(); // get year of current
mon = t.getMonth() + 1; // get month of current
day = t.getDate(); // get date of current
if (MM == 2 && DD == 29){ // check leap year
if (!(((YY % 4 == 0) && (YY % 100 != 0)) || (YY % 400 == 0))){
alert("The year " +YY+ " ends at 28th of "+MM+" month\nPlease
check the date.");
return -1;
}
}
age = year - YY;
if ((MM > mon) || (MM == mon && day < DD)) age --;
return age;
}