Thursday, March 31, 2011

What is the best way to calculate Age using Flex?

What is the best way to calculate Age using Flex?

From stackoverflow
  • 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.

  • 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--;
    
  • 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";
    }
    
  • 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;
    
  • Here's a one-liner:

    int( now.getFullYear() - dob.getFullYear() + (now.getMonth() - dob.getMonth())*.01 + (now.getDate() - dob.getDate())*.0001 );
    
  • The first answer is incorrect. The last comparison should be:

    if (dob.month > now.month || (dob.month == now.month && dob.date >= now.date)) 
    {
       yearsOld--;
    }
    
  • 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--; }

  • 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;
    }
    
  • 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;
    }
    

0 comments:

Post a Comment