Click here to Skip to main content
15,868,055 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
hi,

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

C#
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();
            }
        }
    }
}



Thanks in advance
Anvas
Posted
Updated 10-Oct-19 1:50am
Comments
walterhevedeich 21-Jul-11 1:43am    
the problem is that if today is the birthday i wont count the age
How is that a problem? Of course you would not count the age if DOB is today, as it will be 0 anyway. Or did I just not understand the question correctly?
anvas kuttan 21-Jul-11 2:21am    
working fine in all cases but birthday is current date it shows 0

ie, 18/7/2011 current date
18/7/2010 date of birth
age shows 0

:(
Divyam Sharma 25-Apr-15 5:54am    
Not up to the mark
Prashant_Datir 7-Jul-15 9:35am    
What 365.25 is used for ?

you try this:-
XML
<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>
 
Share this answer
 
Comments
Ankur\m/ 21-Jul-11 1:53am    
The question asks for an issue in HIS code. OP hasn't ask for YOUR code. There are thousands of such example available on the wen.
Try also
HTML
<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>
 
Share this answer
 
hi,
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>
 
Share this answer
 
HI,

C#
function CalculateAgeInQC(DOB, txtAge, Txndate) {
        if (DOB.value != '') {
            //now1 = new Date();alert(now1);
            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 - 1, dob[0]);
                if (now.getMonth() == born.getMonth() && now.getDate() == born.getDate()) {
                    age = now.getFullYear() - born.getFullYear();
                }
                else {
                    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();
                }
            }
        }
    }



i find it by myself i am posting this answer for some may be need the same

thank you for all
 
Share this answer
 
//DOB textbox Id where date of birth from calender is choose
//Age textbox where age after calculate is shown
//Use Jquery Referece file

$(document).ready(function () {
            $("#DOB").change(function () {
                var start_date = new Date($("#DOB").val());
                var date = new Date();
                var end_date = new Date(start_date);
               end_date.setFullYear(date.getFullYear()-start_date.getFullYear());
                $("#Age").val(end_date.getFullYear());
            });
        })
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900