Javascript从出生日期算起的年龄

时间:2011-04-26 05:07:43

标签: javascript

我将我的日历选中date of birth传递给以下JS函数以计算年龄:

var DOBmdy = date.split("-"); 
    Bdate = new Date(DOBmdy[2],DOBmdy[0]-1,DOBmdy[1]); 
    BDateArr = (''+Bdate).split(' '); 
    //document.getElementById('DOW').value = BDateArr[0]; 
    Cdate = new Date; 
    CDateArr = (''+Cdate).split(" ");
    Age = CDateArr[3] - BDateArr[3]; 

现在,让我们说,输入年龄为:2nd Aug 1983,年龄计数为:28,而8月份尚未过去,我想显示当前年龄{{1}而不是27

任何想法,我怎么能写出那个逻辑,用我的JS函数来计算年龄28 完全

谢谢!

4 个答案:

答案 0 :(得分:10)

出生日期成为1983年8月的第二天,然后是那个日期之间的毫秒差异:

var diff = new Date - new Date('1983-08-02');

天数的差异是(1秒= 1000毫秒,1小时= 60 * 60秒,1天= 24 * 1小时)

var diffdays = diff / 1000 / (60 * 60 * 24);

年数(因此,年龄)的差异变为(.25来说明是一年):

var age = Math.floor(diffdays / 365.25);

现在尝试

diff = new Date('2011-08-01') - new Date('1983-08-02'); //=> 27
diff = new Date('2011-08-02') - new Date('1983-08-02'); //=> 28
diff = new Date('2012-08-02') - new Date('1983-08-02'); //=> 29

因此,您的javascript可以重写为:

var Bdate   = new Date(date.split("-").reverse().join('-')),
    age     = Math.floor( ( (Cdate - Bdate) / 1000 / (60 * 60 * 24) ) / 365.25 );

[编辑]没有引起足够的重视。 date.split('-')给出了数组[dd,mm,yyyy],因此将其反转会生成[yyyy,mm,dd]。现在使用' - '再次加入,结果是字符串'yyyy-mm-dd',它是新Date的有效输入。

答案 1 :(得分:2)

(new Date() - new Date('08-02-1983')) / 1000 / 60 / 60 / 24 / 365.25

这会让你在几年内有所不同,你会偶尔使用它来解决一天一天的问题。

答案 2 :(得分:2)

可能是这样的:

    var today = new Date(); 
    var d = document.getElementById("dob").value;
    if (!/\d{4}\-\d{2}\-\d{2}/.test(d)) {   // check valid format
    return false;
    }
    d = d.split("-");
    var byr = parseInt(d[0]); 
    var nowyear = today.getFullYear();
    if (byr >= nowyear || byr < 1900) {  // check valid year
    return false;
    }
    var bmth = parseInt(d[1],10)-1;  
    if (bmth<0 || bmth>11) {  // check valid month 0-11
    return false;
    }
    var bdy = parseInt(d[2],10); 
    if (bdy<1 || bdy>31) {  // check valid date according to month
    return false;
    }
    var age = nowyear - byr;
    var nowmonth = today.getMonth();
    var nowday = today.getDate();
    if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
    else if (bmth == nowmonth && nowday < bdy) {age = age - 1}

    alert('You are ' + age + ' years old'); 

答案 3 :(得分:0)

我只需要编写一个函数来完成这个操作,我想我会分享。

人类的角度来看,这是准确的!没有疯狂的365.2425东西。

var ageCheck = function(yy, mm, dd) {
    // validate input
    yy = parseInt(yy,10);
    mm = parseInt(mm,10);
    dd = parseInt(dd,10);   
    if(isNaN(dd) || isNaN(mm) || isNaN(yy)) { return 0; }   
    if((dd < 1 || dd > 31) || (mm < 1 || mm > 12)) { return 0; }

    // change human inputted month to javascript equivalent 
    mm = mm - 1;

    // get today's date
    var today = new Date();
    var t_dd = today.getDate();
    var t_mm = today.getMonth();
    var t_yy = today.getFullYear(); 

    // We are using last two digits, so make a guess of the century 
    if(yy == 0) { yy = "00"; }
    else if(yy < 9) { yy = "0"+yy; }    
    yy = (today.getFullYear() < "20"+yy ? "19"+yy : "20"+yy);

    // Work out the age!
    var age = t_yy - yy - 1; // Starting point
    if( mm < t_mm ) { age++;} // If it's past their birth month
    if( mm == t_mm && dd <= t_dd) { age++; } // If it's past their birth day

    return age;
}