12,396,146 members (61,920 online)
Rate this:
See more:
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);
age = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
if (isNaN(age) || age < 0) {
}
else {
document.getElementById(txtAge).value = age;
document.getElementById(txtAge).focus();
}
}
}
}```

Anvas
Posted 20-Jul-11 19:35pm
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 ?

Rate this:

## Solution 1

you try this:-
```<html>
<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>
<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>```
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.
Rate this:

## Solution 2

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; }
return age;}</script>
Rate this:

## Solution 3

Try also
```<html>
<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>
<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>```
Rate this:

## Solution 4

HI,

```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 - 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) {
}
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

Top Experts
Last 24hrsThis month
 OriginalGriff 130 Jochen Arndt 85 ppolymorphe 80 BillWoodruff 70 0x01AA 50
 OriginalGriff 6,128 ppolymorphe 2,430 Karthik Bangalore 2,382 F-ES Sitecore 1,877 Richard MacCutchan 1,752