2014-12-05 111 views
0

我试着去计算用户的日期,但它不是计算正确的方式的Javascript - 计算年龄

这里是我的代码

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); 

我的输出是13,但它应该是24

BUT

如果我改变DD,毫米,YY和硬编码这样

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

然后它的工作和输出是24年。 我在这里做错了什么?

编辑

我已经试过这个代替,但我仍然得到114,如果我的一年是1990年,但如果我硬派日期的罚款: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); 
+0

我喜欢这一个:http://stackoverflow.com/a/15555947/265165这是你可以得到的最短答案。 – thmshd 2014-12-05 12:13:49

+0

var myAge = curYear%year;真的吗?如果您出生于1900年,则返回114 – 2014-12-05 12:14:45

+0

您将年份设置为1900年或2000年。您的意思是更多类似于“var year = 1900 + yy”和2000年相同的内容。 – thmshd 2014-12-05 12:18:20

回答

0

这里有一个办法:

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))); 

的解决方案存在的位置:javascript - Age calculation

另一种解决方案:

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

例子:

var age = calculateAge("02/24/1991", "02/24/2010"); // Format: MM/DD/YYYY 
+0

目前生日时不起作用,例如'getAge(new Date(1988,8,7),new Date(2014,8,7))'给出25当它应该给26 – Esailija 2014-12-05 13:01:52

+0

小心一个月。 JavaScript统计他们从0 1978,10,3意味着1978年11月3日。无论如何,我编辑了我的答案,我添加了其他解决方案 – MartaGom 2014-12-05 13:06:12

0

另外,您可以:

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>