Calculate Age in JS
/**
* Convert Birthday to age
*/
function Birthdaytoage () {
var adate=new Date ();
var thisyear = Adate.getfullyear ();
var thismonth = Adate.getmonth () +1;
var thisday = Adate.getdate ();
var currentdate = thisyear+ "-" +thismonth+ "-" +thisday;
var otable = document.getElementById (' Familymember ');
var message = $ ("#message"). html ();
if (message! = NULL && Message! = "") {
return null;
}
for (i=1;i<otable.rows.length;i++) {
var cell = otable.rows[i].cells[3].innerhtml;
if (cell!= "" && cell! = null) {
Age = Daysbetween (Currentdate,cell.trim ());
Otable.rows[i].cells[3].innerhtml = age;
}else{
otable.rows[i].cells[3].innerhtml = "";
}
}
}
/** calculates the age of two days */
function Daysbetween (dateone,datetwo) {
var onemonth = dateone.substring (5,dateone.lastindexof ('-'));
var oneday = dateone.substring (Dateone.length,dateone.lastindexof ('-') +1);
var oneyear = dateone.substring (0,dateone.indexof ('-'));
var twomonth = datetwo.substring (5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring (Datetwo.length,datetwo.lastindexof ('-') +1);
var twoyear = datetwo.substring (0,datetwo.indexof ('-'));
var cha= ((Date.parse (onemonth+ '/' +oneday+ '/' +oneyear)-date.parse (twomonth+ '/' +twoday+ '/' +twoyear))/86400000/365 );
var age = Math.Abs (cha) +1+ "";
Return Age.split (".") [0];
}
Computing Age in Java
/** Calculation Age */
Public String getage (Date birthDay) throws Exception {
Calendar cal = Calendar.getinstance ();
if (Cal.before (BirthDay)) {
throw New IllegalArgumentException (
"The BirthDay is before now.it ' s unbelievable!");
}
int yearnow = Cal.get (calendar.year);
int monthnow = Cal.get (calendar.month) +1;
int dayofmonthnow = Cal.get (calendar.day_of_month);
Cal.settime (BirthDay);
int Yearbirth = Cal.get (calendar.year);
int Monthbirth = Cal.get (calendar.month);
int Dayofmonthbirth = Cal.get (calendar.day_of_month);
int age = Yearnow-yearbirth;
if (Monthnow <= Monthbirth) {
if (Monthnow = = Monthbirth) {
Monthnow==monthbirth
if (Dayofmonthnow < Dayofmonthbirth) {
age--;
}
} else {
Monthnow>monthbirth
age--;
}
}
Return age + "";
}
Calculating Age in Js/java