Calculating Age in Js/java

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.