0

Im trying to calculate the date of a user but its not calculating the right way

here is my code

var cpr = cprValue.toString();

// cpr is = 0102907896

var dd = cpr.substr(0, 2); // 01
var mm = cpr.substr(2, 2); // 02
var yy = cpr.substr(4, 2); // 90

// Calculate if 90 is > 50
if (yy > 50) {
  var year = 1900;
} else {
  var year = 2000;
}


var curDate  = new Date();
var curYear  = curDate.getUTCFullYear(),
curMonth = curDate.getUTCMonth(),
curDay   = curDate.getUTCDate();


var myAge = curYear % year;

if (curMonth < mm && curDay < dd || curMonth < mm && curDay === dd || curMonth == mm && `curDay < dd) {`
  myAge  -= 1;
}

console.log(myAge);

My output is 13 but it should be 24

BUT

if i change the dd,mm,yy and hardcode it like this

var dd = 01;
var mm = 02;
var year = 1990;

Then it works and output is 24 years. what im i doing wrong here?

EDIT

I have tried this instead, but i still get 114 if my year is 1990, but if i hardcore the date its fine :S

var cpr = cprValue.toString();

//cpr is = 0102907896

var dd = cpr.substr(0, 2); // 01
var mm = cpr.substr(2, 2); // 02
var yy = cpr.substr(4, 2); // 90

//Calculate if 90 is > 50
if (yy>50) {
  var year = 1900;
} else {
  var year = 2000;
}

var final = year + "-" + mm + "-" + dd;
console.log("Final: "+ final);

var alder = moment().diff(final, 'years');
console.log(alder);
Eugene Mihaylin
  • 1,736
  • 3
  • 16
  • 31
sij
  • 123
  • 1
  • 3
  • 10
  • I like this one: http://stackoverflow.com/a/15555947/265165 this is the shortest answer you can get. – thmshd Dec 05 '14 at 12:13
  • var myAge = curYear % year; really ? it returns 114 if you've born in 1900's – Emre Acar Dec 05 '14 at 12:14
  • You set the year to either 1900 or to 2000. You mean more something like "var year = 1900 + yy" and same for 2000. – thmshd Dec 05 '14 at 12:18

2 Answers2

0

Here is a way:

function getAge(d1, d2){
    d2 = d2 || new Date();
    var diff = d2.getTime() - d1.getTime();
    return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}
console.log( getAge(new Date(1978, 10, 3)) );

The solution exist here: javascript - Age calculation

Another solution:

function calculateAge (birthDate, otherDate) {
    birthDate = new Date(birthDate);
    otherDate = new Date(otherDate);

    var years = (otherDate.getFullYear() - birthDate.getFullYear());

    if (otherDate.getMonth() < birthDate.getMonth() || 
        otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
        years--;
    }

    return years;
}

Example:

var age = calculateAge("02/24/1991", "02/24/2010"); // Format: MM/DD/YYYY
Community
  • 1
  • 1
MartaGom
  • 501
  • 6
  • 27
  • Doesn't work when it's currently the birthday, e.g. `getAge(new Date(1988, 8, 7), new Date(2014, 8, 7))` gives 25 when it should give 26 – Esailija Dec 05 '14 at 13:01
  • Be careful with the month. Javascript counts them from 0. 1978, 10, 3 means the November 3th, 1978. Anyway i edited my answer and i added other solution – MartaGom Dec 05 '14 at 13:06
0

Alternatively you can:

var result = document.querySelector('#results');

result.innerHTML += 'birthDate 2002/11/11, age today: '+ 
                    age(new Date('2002/11/11'));
result.innerHTML += '<br>birthDate 1944/12/15, age today: '+ 
                    age(new Date('1944/12/11'));
result.innerHTML += '<br>birthDate 1954/12/04, age today: '+ 
                    age(new Date('1954/12/04'));
result.innerHTML += '<br>birthDate 1954/12/06 age on 2013/12/03: '+ 
                    age(new Date('1954/12/06'), new Date('2013/12/03'));


function age(birthDate, forDate) {
   forDate = forDate || new Date;
   
   return (forDate.getFullYear() - birthDate.getFullYear())
          +
          (forDate.getMonth() < birthDate.getMonth() || forDate.getMonth() == birthDate.getMonth() && forDate.getDate() < birthDate.getDate() ? -1 : 0);
}
<div id="results"></div>
Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177