HOWTO: Using AS3 to Get Age from Birthday (Birthdate)

I had to implement a get age method for something we were working on. I was surprised to not find an AS3 version easily, so busted one out. I thought maybe this would be in as3corelib but nope. So posting this in case anyone else is looking for a get age script in AS3. It was quick so any improvements just post them. It will be added to my utilities classes on google code soon it is in quick form so you can jsut plop in an .fla or a class.

The pseudo code is the same for getting age in any language. Get current date, get birthdate to compare. Subtract the current years from the birthday years, then subtract one more year if the day or month is less than today’s day and month.


function calculateAge(birthdate:Date):Number {
    var dtNow:Date = new Date();// gets current date
    var currentMonth:Number = dtNow.getMonth();
    var currentDay:Number = dtNow.getDay();
    var currentYear:Number = dtNow.getFullYear();

    var bdMonth:Number = birthdate.getMonth();
    var bdDay:Number = birthdate.getDay();
    var bdYear:Number = birthdate.getFullYear();

    // get the difference in years
    var years:Number = dtNow.getFullYear() - birthdate.getFullYear();
    // subtract another year if we're before the
    // birth day in the current year
    if (currentMonth < bdMonth || (currentMonth == bdMonth && currentDay < bdDay)) {
        years--;
    }
    return years;
}

function dateStringToObject(dateString):Date {
    var date_ar = dateString.split("/");
    return new Date(date_ar[2],date_ar[0] - 1,date_ar[1]);
}

var dateNow:Date = new Date();
var checkDate:String = "11/25/1976";
var dateBirthday:Date = dateStringToObject(checkDate);
trace("dateNow = "+dateNow);
trace("dateBirthday = "+dateBirthday);
trace("age = "+calculateAge(dateBirthday));

Tags: , , , ,

  • Joseph

    Here’s a simpler version…

    function calculateAge(birthdate:Date):int {
    return Math.floor( ((new Date()).getTime()-birthdate.getTime())/31536000000 );
    }

    OK, maybe not as leap year friendly, but it works.

  • http://labs.hellokeita.com keita

    How about this?
    it’s more accurate.

    var bDate:Date = new Date(1978, 3, 28);
    var today:Date = new Date();
    var age:Date = new Date(today.time – bDate.time);
    age.fullYear -= 1970;
    trace(age);

  • GB

    Bugaga, am/kg :o)
    Joseph +1

  • http://redblind.com/blog/ jcip

    On lines 4 and 8 you should be using getDate() for the numerical day of the month. getDay() returns the numerical day of the week.

    Other than that, thanks!

  • Ormus

    the chalange it’s to get age like years, months, days. Thx.

  • Ormus

    *challenge

  • http://www.caricaturesnow.com Seba

    Hi Guys, I am using this in an application we have developed. Just based on initial code. It may need some cleanup and optimization, but wanted to put it in here as a beta version :)

    var dateNow:Date = new Date();
    var checkDate:String = “12/22/2000″;
    var dateBirthday:Date = dateStringToObject(checkDate);
    trace(“dateNow = “+dateNow);
    trace(“dateBirthday = “+dateBirthday);
    calculateAge(dateBirthday)

    function calculateAge(birthdate:Date):Number {
    var dtNow:Date = new Date();// gets current date
    var currentMonth:Number = dtNow.getMonth() + 1 ;
    var currentDay:Number = dtNow.getDate();
    var currentYear:Number = dtNow.getFullYear();

    var bdMonth:Number = birthdate.getMonth() + 1;
    var bdDay:Number = birthdate.getDate();
    var bdYear:Number = birthdate.getFullYear();

    // get the difference in years
    var years:Number = dtNow.getFullYear() – birthdate.getFullYear();
    var months:Number
    var weeks:Number
    var days:Number

    // subtract another year if we’re before the
    // birth day in the current year
    if (currentMonth < bdMonth || (currentMonth == bdMonth && currentDay bdMonth){
    months = currentMonth – bdMonth – 1

    if(currentDay > bdDay){
    months++
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7

    }else if(currentDay == bdDay){
    months++
    days = 0;
    weeks = 0
    }else{// currentDay is smaller than bdDay
    // this is not trivial
    // here we calculate the days from the bdDay of the previous month + currentDay
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }else if(currentMonth == bdMonth){

    if(currentDay > bdDay){
    months = 0
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7
    }else if(currentDay == bdDay){
    months = 0
    days = 0
    weeks = 0
    }else{// currentDay is smaller than bdDay
    months = 11
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }else{ // this is when the currentMonth is smaller than bdMonth
    months = (12 – bdMonth) + (currentMonth – 1)

    if(currentDay > bdDay){
    months++
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7
    }else if(currentDay == bdDay){
    months++
    days = 0
    weeks = 0
    }else{// currentDay is smaller than bdDay
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }

    trace (“age:” + years)
    trace (“months:” + months)
    trace (“weeks:” + weeks)
    trace (“days:” + days)

    //var time:String = years + ” years, ” + months + ” months, ” + weeks + ” weeks, ” + days + ” days old” ;

    //time_txt.text = time;
    }

    function getDays(previousMonth:Number, nowDate:Date):Number{

    var residual:Number = nowDate.getFullYear()%4
    trace(“Is leap =” + residual)

    trace(“previousMonth=” + previousMonth )
    switch(previousMonth){
    case 0://December
    return 31
    break
    case 1://Jaunary
    return 31
    break
    case 2://February
    if (residual == 0){
    return 29
    }else{
    return 28
    }
    break

    case 3://March
    return 31
    break
    case 4://April
    return 30
    break
    case 5://May
    return 31
    break

    case 6://June
    return 30
    break
    case 7://Jul
    return 31
    break
    case 8://Aug
    return 31
    break

    case 9://Sep
    return 30
    break
    case 10://Oct
    return 31
    break
    case 11://Nov
    return 30
    break
    }

    return 0
    }

    function dateStringToObject(dateString):Date {
    var date_ar = dateString.split(“/”);
    return new Date(date_ar[2],date_ar[0] – 1,date_ar[1]);
    }

  • http://www.caricaturesnow.com Seba

    A new version, previous one had an error, please ignore previous post.

    Thanks

    var dateNow:Date = new Date();
    var checkDate:String = “12/22/2000″;
    var dateBirthday:Date = dateStringToObject(checkDate);
    trace(“dateNow = “+dateNow);
    trace(“dateBirthday = “+dateBirthday);
    calculateAge(dateBirthday)

    function calculateAge(birthdate:Date):void {
    var dtNow:Date = new Date();// gets current date
    var currentMonth:Number = dtNow.getMonth() + 1 ;
    var currentDay:Number = dtNow.getDate();
    var currentYear:Number = dtNow.getFullYear();

    var bdMonth:Number = birthdate.getMonth() + 1;
    var bdDay:Number = birthdate.getDate();
    var bdYear:Number = birthdate.getFullYear();

    // get the difference in years
    var years:Number = dtNow.getFullYear() – birthdate.getFullYear();
    var months:Number
    var weeks:Number
    var days:Number

    // subtract another year if we’re before the
    // birth day in the current year
    if (currentMonth < bdMonth || (currentMonth == bdMonth && currentDay bdMonth){ // Current Mont is Bigger than Birthday Month

    months = currentMonth – bdMonth – 1
    if(currentDay > bdDay){
    months++
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7

    }else if(currentDay == bdDay){
    months++
    days = 0;
    weeks = 0
    }else{// currentDay is smaller than bdDay
    // this is not trivial
    // here we calculate the days from the bdDay of the previous month + currentDay
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }else if(currentMonth == bdMonth){

    if(currentDay > bdDay){
    months = 0
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7
    }else if(currentDay == bdDay){
    months = 0
    days = 0
    weeks = 0
    }else{// currentDay is smaller than bdDay
    months = 11
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }else{ // this is when the currentMonth is smaller than bdMonth
    months = (12 – bdMonth) + (currentMonth – 1)

    if(currentDay > bdDay){
    months++
    days = currentDay – bdDay
    weeks = Math.floor(days / 7)
    days %=7
    }else if(currentDay == bdDay){
    months++
    days = 0
    weeks = 0
    }else{// currentDay is smaller than bdDay
    days = getDays(currentMonth – 1, dtNow) – bdDay + currentDay
    weeks = Math.floor(days / 7)
    days %=7
    }
    }

    trace (“age:” + years)
    trace (“months:” + months)
    trace (“weeks:” + weeks)
    trace (“days:” + days)

    //var time:String = years + ” years, ” + months + ” months, ” + weeks + ” weeks, ” + days + ” days old” ;
    }

    function getDays(previousMonth:Number, nowDate:Date):Number{

    var isLeap:Boolean = (nowDate.getFullYear()%4) == 0
    trace(“Is leap =” + isLeap)
    trace(“previousMonth=” + previousMonth )
    switch(previousMonth){
    case 0:/*Dec*/ return 31
    case 1:/*Jan*/ return 31
    case 2:/*Feb*/ if (isLeap == 0){ return 29 }else{ return 28 }
    case 3:/*Mar*/ return 31
    case 4:/*Apr*/ return 30
    case 5:/*May*/ return 31
    case 6:/*Jun*/ return 30
    case 7:/*Jul*/ return 31
    case 8:/*Aug*/ return 31
    case 9:/*Sep*/ return 30
    case 10:/*Oct*/ return 31
    case 11:/*Nov*/ return 30
    }
    return 0
    }

    function dateStringToObject(dateString):Date {
    var date_ar = dateString.split(“/”);
    return new Date(date_ar[2],date_ar[0] – 1,date_ar[1]);
    }