Click here to Skip to main content
11,710,824 members (85,017 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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

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 20-Jul-11 19:35pm
Comments
walterhevedeich at 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 at 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 at 25-Apr-15 5:54am
   
Not up to the mark
Prashant_Datir at 7-Jul-15 9:35am
   
What 365.25 is used for ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
Comments
Ankur\m/ at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

HI,

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 549
1 OriginalGriff 270
2 Peter Leow 134
3 Abhinav S 130
4 Richard MacCutchan 80
0 Abhinav S 60
1 Member 4653839 40
2 barneyman 35
3 Sergey Alexandrovich Kryukov 35
4 Member 11793820 35


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 21 Jul 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100