Comparing Age to a Range of Ages

Permabanned
Joined
25 Oct 2004
Posts
9,078
Hiya,

Looking for an example or even working code in java that will take a date of birth and compare that persons age with a set number of age ranges and place them in the correct one. My knowledge of java is very limited so i havent a clue where to start :(

Cheers
 
Last edited:
Code:
var iAge = 26

if (iAge >= 0 && iAge <= 10) {
  alert("you are aged between 0 and 10")
}
else if (iAge >= 11 && iAge <= 20) {
  alert("you are aged between 11 and 20")
}
else if (iAge >= 21 && iAge <= 30) {
  alert("you are aged between 21 and 30")
}

*ninja*
Crap - just noticed you said java, not javascript. Either way, it shouldn't be too far removed from the above.

*not so ninja*
I am a tool.
 
Last edited:
Had a little play around but cant seem to get it working, the date part functions ok as i can use an alert to output the age and it works fine.

But what i want is for it to compare the age with the if statements and then change the value of a text field to how the age range.

Here is what i have so far, bare in mind my knowledge of JS is minimal so its probably total garbage! :P

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function showAge(){
var d =document.getElementById('txt').value.split('/');
var today=new Date();
var bday=new Date(d[2],d[1],d[0]);
var by=bday.getFullYear();
var bm=bday.getMonth()-1;
var bd=bday.getDate();
var age=0; var dif=bday;

while(dif<=today){
var dif = new Date(by+age,bm,bd);
age++;
}

age +=-2 ;

if (age >= 0 && age <= 16) {
    document.getElementById(ager).value = '0 to 16';
    }
    else if (age >= 17 && age <= 24) {
      document.getElementById(ager).value = '17 to 24';
    }
    else if (age >= 25 && age <= 34) {
      document.getElementById(ager).value = '25 to 34';
    }
    else if (age >= 35 && age <= 44) {
      document.getElementById(ager).value = '35 to 44';
    }
    else if (age >= 45 && age <= 54) {
      document.getElementById(ager).value = '45 to 54';
    }
    else if (age >= 55 && age <= 64) {
      document.getElementById(ager).value = '55 to 64';
    }
    else if (age >= 65) {
      document.getElementById(ager).value = '65+';
    }
}
</script>
</head>
<body>
Insert your date of birth in format dd/mm/yyyy<br>
<input id="txt" type="text" value="" onclick="showAge()"/>
<input id="ager" type="text" disabled="true" value=""/>
</body>
</html>
 
You're missing the ' from the ager bit.
Anyway. Here is another way to calculate the age which doesn't involve what could be a massive loop.
Code:
<html>
<head>
<script type="text/javascript">
<!--
function showAge(){
  now = new Date()
  var d =document.getElementById('txt').value.split('/');
 
 if(d.length==3){
   born = new Date(d[2], d[1]*1-1, d[0]);
   years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
   
   var Range = "None";
   if (years >= 0 && years <= 16) {
    Range = '0 to 16';
    }
    else if (years >= 17 && years <= 24) {
      Range = '17 to 24';
    }
    else if (years >= 25 && years <= 34) {
      Range = '25 to 34';
    }
    else if (years >= 35 && years <= 44) {
      Range = '35 to 44';
    }
    else if (years >= 45 && years <= 54) {
      Range = '45 to 54';
    }
    else if (years >= 55 && years <= 64) {
      Range = '55 to 64';
    }
    else if (years >= 65) {
      Range = '65+';
    }

document.getElementById("ager").value = Range;
 }
}
// -->
</script>
</head>
<body>
<form>
Insert your date of birth in format dd/mm/yyyy<br>
<input id="txt" type="text" value="" onclick="showAge()"/>
<input id="ager" type="text" disabled="true" value=""/>

</form>


</body>
</html>
 
Back
Top Bottom