使用Javscript或Jquery计算出生日期的年龄 [英] Calculate Age from Date of birth using Javscript or Jquery

查看:81
本文介绍了使用Javscript或Jquery计算出生日期的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何计算从出生日期起的年龄到目前为止,我做了一个工作正常的代码,但问题是如果今天是生日我不会算上年龄



这里是代码





how can calculate age from date of birth so far i done a code which works fine but the problem is that if today is the birthday i wont count the age

here is code

function CalculateAgeInQC(DOB, txtAge, Txndate) {
    if (DOB.value != '') {
        now = new Date(Txndate)
        var txtValue = document.getElementById(DOB).value;
        if (txtValue != null)
            dob = txtValue.split('/');
        if (dob.length === 3) {
            born = new Date(dob[2], dob[1] * 1 , dob[0]);
            age = now.getTime() - (born.getTime()) / (365.25 * 24 * 60 * 60 * 1000);
            alert(" now.getTime  " + now.getTime());
            alert(" born.getTime  " + born.getTime());
            age = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
            if (isNaN(age) || age < 0) {
                // alert('Input date is incorrect!');
            }
            else {
                document.getElementById(txtAge).value = age;
                document.getElementById(txtAge).focus();
            }
        }
    }
}







提前致谢

Anvas




Thanks in advance
Anvas

推荐答案

你试试这个: -

you try this:-
<html>
<head>
<script language="javascript">
<!--
function Age()
{
var bday=parseInt(document.forms[0].txtBday.value);
var bmo=(parseInt(document.forms[0].txtBmo.value)-1);
var byr=parseInt(document.forms[0].txtByr.value);
var byr;
var age;
var now = new Date();
tday=now.getDate();
tmo=(now.getMonth());
tyr=(now.getFullYear());
{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{age=byr}
else
{age=byr+1}
alert("As of today, "+now+' \n'+", you are:"+(tyr-age)+ " years old");
}}
//-->
</script>
</head>
<body >
<form><center>
Enter your birthday&nbsp;&nbsp;<input type="text" name="txtBday" size="2"><br/>
Enter your birth Month(1-12)<input type="text" name="txtBmo"size="2"><br/>
Enter your 4 digit birth year<input type="text" name="txtByr"size="4" ><br/>
<input type="submit" value="submit" onClick="Age()"><input type="reset" value="reset"></center>
</form>
</body>
</html>


还可以试试

Try also
<html>
<head>
<script type="text/javascript" language="javascript">
function calcAge(dtFrom, dtTo)
{
    var a = dtTo.getDate() + (dtTo.getMonth() + (dtTo.getFullYear() - 1700) * 16) * 32;
    var b = dtFrom.getDate() + (dtFrom.getMonth() + (dtFrom.getFullYear() - 1700) * 16) * 32;
    var x = Math.floor((a - b) / 32 / 16);
    return x < 0 ? null : x;
}
function calc()
{
    var dtTo = new Date(document.getElementById('enddate').value);
    var dtFrom = new Date(document.getElementById('startdate').value);
    document.getElementById('diff').value = calcAge(dtFrom, dtTo);
    return false;
}
</script>
</head>
<body>
Start Date:<input type='text' id='startdate' value='03/21/1972'/><br/>
End Date:<input type='text' id='enddate' value='03/21/1979'/><br/>
Years:<input type='text' id='diff' value=''/><br/>
<input type='button' onclick='javascript:calc();' value='Calculate'/>
</body>
</html>


hi ,

此代码将帮助您



< script type =text / javascript>

function calculate_age(birth_month,birth_day,birth_year)

{today_date = new Date();

today_year = today_date.getYear();

today_month = today_date.getMonth();

today_day = today_date.getDate();

age =(today_year + 1900) - birth_year;

if(today_month<(birth_month - 1))

{age--; } if(((birth_month - 1)== today_month)&&(today_day< birth_day))

{age--; }

if(age> 1900)

{age - = 1900; }

返回年龄;}< / script>

this code will help you

<script type="text/javascript">
function calculate_age(birth_month,birth_day,birth_year)
{ today_date = new Date();
today_year = today_date.getYear();
today_month = today_date.getMonth();
today_day = today_date.getDate();
age = (today_year + 1900) - birth_year;
if ( today_month < (birth_month - 1))
{ age--; } if (((birth_month - 1) == today_month) && (today_day < birth_day))
{ age--; }
if (age > 1900)
{ age -= 1900; }
return age;}</script>


这篇关于使用Javscript或Jquery计算出生日期的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆