views:

1970

answers:

9

What is the best way to calculate Age using Flex?

+7  A: 

I found an Answer at the bottom of this page in comments section

Repeated here for posterity (should the original comment be lost)

jpwrunyan said on Apr 30, 2007 at 10:10 PM :

By the way, here is how to calculate age in years (only) from DOB without needing to account for leap years:

private function getYearsOld(dob:Date):uint {
var now:Date = new Date();
var yearsOld:uint = Number(now.fullYear) - Number(dob.fullYear);
if (dob.month >= now.month && dob.date >= now.date) {
yearsOld--;
}
return yearsOld;
}

This handles most situations where you need to calculate age.

Richard Braxton
+1  A: 

You could also do it roughly the same as discussed here: (translated to AS3)

var age:int = (new Date()).fullYear - bDay.fullYear;
if ((new Date()) < (new Date((bDay.fullYear + age), bDay.month, bDay.date))) age--;
81bronco
+1  A: 

Here is a little more complex calculation, this calculates age in years and months. Example: User is 3 years 2 months old.

private function calculateAge(dob:Date):String {        
    var now:Date = new Date();

    var ageDays:int = 0;
    var ageYears:int = 0;
    var ageRmdr:int = 0;

    var diff:Number = now.getTime()-dob.getTime();
    ageDays = diff / 86400000;
    ageYears = Math.floor(ageDays / 365.24);
    ageRmdr = Math.floor( (ageDays - (ageYears*365.24)) / 30.4375 );

    if ( ageRmdr == 12 ) {
     ageRmdr = 11;
    }

    return ageYears + " years " + ageRmdr + " months";
}
maclema
+3  A: 
var userDOB : Date = new Date(year,month-1,day);
var today : Date = new Date();

var diff : Date = new Date();
diff.setTime( today.getTime() - userDOB.getTime() );

var userAge : int = diff.getFullYear() - 1970;
+1  A: 

Here's a one-liner:

int( now.getFullYear() - dob.getFullYear() + (now.getMonth() - dob.getMonth())*.01 + (now.getDate() - dob.getDate())*.0001 );
datico
A: 

The first answer is incorrect. The last comparison should be:

if (dob.month > now.month || (dob.month == now.month && dob.date >= now.date)) 
{
   yearsOld--;
}
Eduardo Mauro
A: 

The previous correction is still incorrect: you don't need to reduce 1 year on your birthday!

if (dob.month > now.month || (dob.month == now.month && dob.date > now.date)) { yearsOld--; }

Fine-Wei Lin
A: 

I found a few problems with the top answer here. I used a couple of answers here to cobble together something which was accurate (for me anyway, hope for you too!)

private function getYearsOld(dob:Date):uint
{
    var now:Date = new Date();
    var age:Date = new Date(now.getTime() - dob.getTime());
    var yearsOld:uint = age.getFullYear() - 1970;
    return yearsOld;
}
Joe
A: 

I tried to correct the answer from Braxton here. Some people have aldready noticed it, but here is the correction in its context:

private function getYearsOld(dob:Date):uint 
{
  var now:Date = new Date();
  var yearsOld:uint = Number(now.fullYear) - Number(dob.fullYear);

  if (dob.month > now.month || (dob.month == now.month && dob.date > now.date) 
  {
    yearsOld--;
  }
  return yearsOld;
}
Guppel