age calculation

1. Senior Member
Join Date
Dec 1969
Posts
2,493

## age calculation

Does anyone have an age cal script I am trying to use this but sometimes it doesn&#039;t work<BR><BR>&#060;html&#062;<BR>&#060;head&#062;<B R>&#060;script language="javascript"&#062;<BR>function DateDemo()<BR>{<BR> var arrDOB,d, cAge, s = "Today&#039;s date is: "; <BR> d = new Date();<BR> alert(d.getMonth() + 1 + &#039; - &#039; + d.getDate());<BR> dob = document.demo.DOB.value<BR> arrDOB = dob.split(&#039;/&#039;)<BR> cAge = ((d.getYear()* 365 - parseInt(arrDOB[2])* 365)/365 );<BR> alert(cAge);<BR> if(parseInt(d.getMonth() + 1) &#060; parseInt(arrDOB[0]))<BR> {<BR> alert(&#039;Computer month = &#039; + d.getMonth() + &#039; Birth month = &#039; + arrDOB[0])<BR> cAge = parseInt(cAge) - 1<BR> }<BR> else if(parseInt(d.getMonth() + 1) &#062;= parseInt(arrDOB[0]) && parseInt(d.getDate()) &#060; parseInt(arrDOB[1]))<BR> {<BR> alert(&#039;Computer day = &#039; + d.getDate() + &#039; Birth day = &#039; + arrDOB[1])<BR> cAge = parseInt(cAge) - 1<BR> } <BR> return(cAge); //Return date.<BR>}<BR>&#060;/script&#062;<BR>&#060;/head&#062;<BR>&#060;form name="demo"&#062;<BR>&#060;input type="text" name="DOB"&#062;<BR>&#060;input type="button" value="GO" OnClick="alert(DateDemo())"&#062;<BR>&#060;/form&#062;<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## You are ignoring leap years.

Do you want just the number of years? If so, you don&#039;t need any of that crud.<BR><BR>Just subtract the DOB year from the current year, get age.<BR><BR>If the month of DOB is more than current month, subtract one from age.<BR><BR>If the month of DOB is same as current month, and if day of DOB is more than current day, subtract one from age.<BR><BR>Done.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
2,493

## RE: You are ignoring leap years.

I am trying to calculate the users age based on thier date of Birth like you would using datediff in vbscript

4. Senior Member
Join Date
Dec 1969
Posts
2,493

## RE: You are ignoring leap years.

Ok this is what I have and it seems to work but see if you find any flaws<BR><BR>just out of curiousity why do the month go from 0 to 11 in JS<BR><BR>function calcAge(dob)<BR>{<BR> var arrDOB<BR> var d<BR> var cAge;<BR> var sDate = new Date(dob) <BR> d = new Date();<BR> <BR> //alert(d.getMonth() + &#039; - &#039; + sDate.getMonth());<BR><BR> arrDOB = dob.split(&#039;/&#039;)<BR><BR> //alert (parseInt(d.getYear()) + &#039; &#039; + parseInt(arrDOB[2]))<BR><BR> cAge = ((parseInt(d.getYear()) - parseInt(arrDOB[2])));<BR><BR> //alert(&#039;Age = &#039; + cAge);<BR><BR> if(parseInt(sDate.getMonth()) &#062; parseInt(d.getMonth()))<BR> {<BR> //alert(&#039;Computer month = &#039; + d.getMonth() + &#039; Birth month = &#039; + sDate.getMonth())<BR><BR> cAge = parseInt(cAge) - 1<BR> }<BR> else if(parseInt(d.getMonth()) == parseInt(sDate.getMonth()) && parseInt(sDate.getDate()) &#062; parseInt(d.getDate()))<BR> {<BR> //alert(&#039;Computer day = &#039; + d.getDate() + &#039; Birth day = &#039; + sDate.getDate())<BR><BR> cAge = parseInt(cAge) - 1<BR> } <BR> return(cAge);<BR>}

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## You don't NEED parseInt...

...when the value you are getting is *already GUARANTEED* to be an integer. Such as getMonth and getDate.<BR><BR>And why did you do the year differently than month and date???<BR><BR>function calcAge(dob)<BR>{<BR> var cAge;<BR> var sDate = new Date(dob) <BR> today = new Date();<BR><BR> cAge = today.getYear() - sDate.getYear();<BR> if( sDate.getMonth() &#062; today.getMonth() <BR> &#124&#124 ( sDate.getMonth() == today.getMonth()<BR> && sDate.getDate() &#062; today.getDate() <BR> ) <BR> ) --cAge;<BR> return(cAge);<BR>}<BR>

6. Senior Member
Join Date
Dec 1969
Posts
2,493

## RE: You don't NEED parseInt...

because sDate.getYear() was always returning a 2 digit year and throwingoff my calc

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## OH! Just look in the docs...

http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjdate.asp<BR><BR>Look at the third METHOD listed for the Date object on that page.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## And to be fair...

I tend to always forget that getYear() only returns a 2 digit year. Isn&#039;t that idiotic, that they designed it that way?<BR><BR>Blame the Netscape people back in 1994 or so who couldn&#039;t see 6 years ahead.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•