2013-10-25 38 views
0

我試圖做一個JavaScript程序和計算年齡的使用,我已經做了下面的代碼,JavaScript的年齡計算器不斷給錯了數字,

function submitForm() { 
    var d = new Date(); 
    var year = d.getFullYear(); 
    var month = d.getMonth(); 
    var days = d.getDay(); 
    var minutes = d.getMinutes(); 
    var hours = d.getHours(); 
    var byear = document.dataform.year.selectedIndex; 
    var bmonth = document.dataform.month.selectedIndex; 
    var bday = document.dataform.day.selectedIndex; 
    var bhours = bday * 24; 
    var bmin = 60 * b hours; 
    var dyears = year - byear; 
    var dmonth = month - bmonth; 
    var ddays = (days - bday); 
    var dhours = hours - bhours; 
    var dminutes = minutes - bmin; 
    var daysTillBDay = 365 - bday; 
    if (isLeapYear() == true) { 
     dyears = year - byear; 
     dmonth = month - bmonth; 
     ddays = (days - bday) + 1; 
     dhours = hours - bhours; 
     dminutes = minutes - bmin; 
     daysTillBDay = 365 - bday; 
    } 
    var el = document.getElementsByName('uyears'); 
    el[0].value = dyears + " years old."; 
    el = document.getElementsByName('umonths'); 
    el[0].value = dmonth + " months old."; 
    document.getElementsByName('udays')[0].value = ddays; 
    document.getElementsByName('lmonths')[0].value = dmonth; 
    document.getElementsByName('ldays')[0].value = ddays; 
    document.getElementsByName('lhrs')[0].value = dhours; 
    document.getElementsByName('lmin')[0].value = dminutes; 
    document.getElementsByName('bdays')[0].value = daysTillBDay + " days left till your birthday."; 
} 

我想我會的工作,我嘗試之前運行該程序後,我發現該程序給我幾乎everthing錯誤,我有像2013年(最新年)的一年,並且我將等於-1的負數如果我輸入11.我試圖找到用戶的年齡,我認爲這是今天(今天的大門) - (生日)的日期,但現在不工作,anyobne知道什麼>?

+1

我建議使用'.value'而不是'.selectedindex'作爲你的'