views:

98

answers:

4

Hello!

How can I calculate age in years from a date of format YYYYMMDD to today. Is it possible using the Date() class?

Thank you.

EDIT:

I am looking for a better solution than the one I am using now:

var dob='19800810';
var year=Number(dob.substr(0,4));
var month=Number(dob.substr(4,2))-1;
var day=Number(dob.substr(6,2));
var today=new Date();
var age=today.getFullYear()-year;
if(today.getMonth()<month || (today.getMonth()==month && today.getDate()<day)){age--;}
alert(age);
+2  A: 

There you go: http://codingforums.com/showthread.php?t=171435

Google is your friend :)

Júlio Santos
Thank you, Julio. I'll wait and see if that can be done with the Date() class otherwise I will use the if else version where you check year than month then day.
Francisc
+3  A: 

Some time ago I made a function with that purpose:

function getAge(birthDate) {
  var now = new Date();

  function isLeap(year) {
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  }

  // days since the birthdate    
  var days = Math.floor((now.getTime() - birthDate.getTime())/1000/60/60/24);
  var age = 0;
  // iterate the years
  for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++){
    var daysInYear = isLeap(y) ? 366 : 365;
    if (days >= daysInYear){
      days -= daysInYear;
      age++;
      // increment the age only if there are available enough days for the year.
    }
  }
  return age;
}

It takes a Date object as input, so you need to parse the 'YYYYMMDD' formatted date string:

var birthDateStr = '19840831',
    parts = birthDateStr.match(/(\d{4})(\d{2})(\d{2})/),
    dateObj = new Date(parts[1], parts[2]-1, parts[3]); // months 0-based!

getAge(dateObj); // 26
CMS
Ah yes, I missed the leap year. Thank you.
Francisc
+1  A: 

To test whether the birthday already passed or not, I define a helper function Date.prototype.getDoY, which effectively returns the day number of the year. The rest is pretty self-explanatory.

Date.prototype.getDoY = function() {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.floor(((this - onejan) / 86400000) + 1);
};

var getAge = function(birthDate) {
    var now = new Date();
    var age = now.getFullYear() - birthDate.getFullYear();

    if (now.getDoY() < birthDate.getDoY())
        age--;  // birthday not yet passed this year, so -1

    return age;
};

var myBirth = new Date(1979, 4, 10);
console.log(getAge(myBirth));
Marcel Korpel
A: 
function age()
{
    var birthdate = $j('#birthDate').val(); // in   "mm/dd/yyyy" format
    var senddate = $j('#expireDate').val(); // in   "mm/dd/yyyy" format
    var x = birthdate.split("/");    
    var y = senddate.split("/");
    var bdays = x[1];
    var bmonths = x[0];
    var byear = x[2];
    //alert(bdays);
    var sdays = y[1];
    var smonths = y[0];
    var syear = y[2];
    //alert(sdays);

    if(sdays < bdays)
    {
        sdays = parseInt(sdays) + 30;
        smonths = parseInt(smonths) - 1;
        //alert(sdays);
        var fdays = sdays - bdays;
        //alert(fdays);
    }
    else{
        var fdays = sdays - bdays;
    }

    if(smonths < bmonths)
    {
        smonths = parseInt(smonths) + 12;
        syear = syear - 1;
        var fmonths = smonths - bmonths;
    }
    else
    {
        var fmonths = smonths - bmonths;
    }

    var fyear = syear - byear;
    document.getElementById('patientAge').value = fyear+' years '+fmonths+' months '+fdays+' days';
}
Sarwar