计算年龄并将其变为变量

时间:2014-04-22 21:45:35

标签: javascript validation date-of-birth

<script type="text/javascript"> 

         var BDyear = document.getElementById("BDyear").value
         var BDmonth = document.getElementById("BDmonth").value
         var BDday = document.getElementById("BDday").value
         var BDstate = false
         var BDcheck = true
         var Cdate = new Date()
         var Cyear = Cdate.getFullYear()

// sectioning age calulation into months with 28 30 and 31 days
if ((BDyear <= Cyear && BDyear >= 1920) && (BDmonth <= 12 && BDmonth >= 1) && (BDday <= 28 && BDday >= 1) && BDmonth == 2) {
                 document.getElementById("bderror").style.display = "none"
                 //
                 // Age calculation place holder
                 //
                 if (age < 13) {
                     document.getElementById("bderroryoung").display = 'inline'
                     BDstate = false
                 }
                 else {
                     document.getElementById("bderroryoung").display = 'none'
                     BDstate = true
                 }


             }

             if ((BDyear <= Cyear && BDyear >= 1920) && (BDmonth <= 12 && BDmonth >= 1) && (BDday <= 31 && BDday >= 1) && (BDmonth == 1 || BDmonth == 3 || BDmonth == 5 || BDmonth == 7 || BDmonth == 8 || BDmonth == 10 || BDmonth == 12)) {
                 document.getElementById("bderror").style.display = "none"
                 //
                 // Age calculation place holder
                 //
                 if (age < 13) {
                     document.getElementById("bderroryoung").display = 'inline'
                     BDstate = false
                 }
                 else {
                     document.getElementById("bderroryoung").display = 'none'
                     BDstate = true
                 }
             }


             if ((BDyear <= Cyear && BDyear >= 1920) && (BDmonth <= 12 && BDmonth >= 1) && (BDday <= 30 && BDday >= 1) && (BDmonth == 2 || BDmonth == 4 || BDmonth == 6 || BDmonth == 9 || BDmonth == 11)) {
                 document.getElementById("bderror").style.display = "none"
                 //
                 // Age calculation place holder
                 //
                 if (age < 13) {
                     document.getElementById("bderroryoung").display = 'inline'
                     BDstate = false
                 }
                 else {
                     document.getElementById("bderroryoung").display = 'none'
                     BDstate = true
                 }

             }
         }
 </script>

 <input class="text" id="BDyear" maxlength="4" style="width:8%" />&nbsp/&nbsp<input class="text" id="BDmonth" maxlength="2" style="width:5%" />&nbsp/&nbsp<input class="text" id="BDday" maxlength="2" style="width:5%" /><br />
                     <p id="bderror" style="position:absolute; top:70%; color:red; font:65% arial; display:none"> תאריך לידה לא תקין</p>
                     <p id="bderroryoung" style="position:absolute; top:70%; color:red; font:65% arial; display:none"> חובה להיות מעל גיל 13</p>

我查了很多方法来计算出生日期的年龄,他们最终都使用了“返回”,而我是一个完整的新手并且在我的学校项目编程的第一年工作我挣扎着所有这些论点和东西......

任何人都可以根据我的代码向我展示如何插入计算过程以最终确定为数值变量?

注意:这是代码的一小部分,所有对象都已正确定义。出生日期语法为DD / MM / YYYY。

3 个答案:

答案 0 :(得分:0)

您需要一个返回正确值的函数。 e.g:

function returnAge(day, month, year) {
    var theAge;

    // ... do calculations and set theAge

    return theAge;
}

然后你想调用那个函数并将它返回的值放入变量并使用它。

var age = returnAge(BDday, BDmonth, BDyear)
if (age < 13) { /* do something */ }
else { /* do something */ }

答案 1 :(得分:0)

我建议使用date.js这样的日期库。您正在进行大量的手动日期计算,这些计算与Date.js不相关。

http://www.datejs.com/

你可以像这样使用它:

var BDyear = document.getElementById("BDyear").value;
var BDmonth = document.getElementById("BDmonth").value;
var BDday = document.getElementById("BDday").value;

var birthDate = new Date(BDday + '/' + BDmonth + '/' +  BDyear);

if (birthDate != null) {
    var age = calculateAge(birthDate);
}


function calculateAge(birthday) {
    var ageDifMs = Date.now() - birthday.getTime();
    var ageDate = new Date(ageDifMs); 
    return Math.abs(ageDate.getUTCFullYear() - 1970);
}

答案 2 :(得分:0)

你看到很多例子使用&#34; return&#34;同时将代码结构化为函数的优点。这对你学习编程来说是一个很好的下一步。函数将允许您创建可重用且更易于读取和调试的代码块。

回答你的问题:

var BDyear = document.getElementById("BDyear").value
var BDmonth = document.getElementById("BDmonth").value
var BDday = document.getElementById("BDday").value

// create a new date object based on your input
var BDdate  = new Date(BDyear, BDmonth, BDday);

// get today's date
var Today = new Date();

// getTime returns time in milliseconds
var DateDiff = Today.getTime() -BDdate.getTime();

// convert milliseconds to years and return the floor

// milliseconds in a year
var YearMilliseconds = 1000 * 60 * 60 * 24 * 365;

// here is your variable with the age
var Age = Math.floor(DateDiff / YearMilliseconds);

更新

上述情况并未考虑闰年[即使我们将一年中的天数改为365.242199]。感觉更好,因为我们正在做一些有趣的数学,但如果我们只是比较月份和时间,我们就会忘记特殊情况。这样的一天:

var BDdate  = new Date(BDyear, BDmonth, BDday);

// get today's date
var Today = new Date();

// get current month.  adjust by one so it is using same scale as user input [getMonth() returns 0-11);
var TodayMonth = Today.getMonth() + 1;

// difference in years
var Age = Today.getFullYear() -BDdate.getFullYear();

// adjust for month/day
// if the birth month was greater than today, subtract a year
// if the birth month is the same as this one, we need to compare the actual day
if (BDmonth > TodayMonth || (BDmonth === TodayMonth && BDday > Today.getDate())) {
    // oops, it's not our birthday yet, so subtract a year
    Age = Age - 1;    
}

// Age is ready to use

see w3schools for more info on the Date object