如何在JavaScript中计算出生日期与系统日期之间的差异

时间:2013-06-12 07:08:47

标签: javascript

我想用JavaScript计算出生日期和系统日期之间的差异。

JSP和JavaScript中的

'出生日期 “

function currentAge(DateOfBirth){
    //alert(DateOfBirth);
    var date1 = DateOfBirth;
    date1 = document.getElementById("dateOfBirth").value;
    alert(date1);
    var date2 = new Date();
    alert(date2);

    // _Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));

    /* var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
    //var diffDays = date2.getDate() - date1.getDate();  */
    //alert(_Diff);
}

2 个答案:

答案 0 :(得分:0)

function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

date1temp = new Date(dateform.firstdate.value);
date1.setTime(date1temp.getTime());

date2temp = new Date(dateform.seconddate.value);
date2.setTime(date2temp.getTime());
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();
days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
dateform.difference.value = days;
return false; 
}

答案 1 :(得分:0)

现在好了

    function currentAge(DateOfBirth){
        var date1 = document.getElementById("dateOfBirth").value;
           alert(date1);

           var date11=new Date(date1);

           alert(date11);

           var date2 = new Date();
           alert(date2);
           alert(3);
           var timeDiff = Math.abs(date2.getYear() - date11.getYear());

           alert(timeDiff);




     }