Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need som help with calculate age

Status
Not open for further replies.

oil

MIS
Mar 3, 2005
2
GB
Hi,<br>I want to do something with my program. In my program, I have birthdate field that want user to fill and I want to use this birthdate to calculate and show besides birthdate filed that &quot;You are 24 years old&quot; (like this) .So can I write my program to do this (and recalculate whenever users change birthdate).<br><br>Thanks in any advance<br><br>Oil<br><br>
 
use this function. it will return an integer value equal to the persons age, if sent the birthdate.<br><br>function getAge(year,month,day){<br>var age=0;<br>var currentDate=new Date();<br>var date=currentDate.getDate();<br>var yr=currentDate.getYear();<br>var mo=currentDate.getMonth();<br>if (yr&lt;100){<br>yr=&quot;19&quot;+yr;<br>yr=yr.parseInt()<br>//this doublechecks to make sure that the year is in the <br>//correct format, if the user's pc clock is set back in <br>//time.<br>}<br>month=month-1;<br>age=yr-year;<br>if (month&lt;mo) age++;<br>if (month==mo) if (day&lt;=date) age++;<br><br>return age;<br>}<br><br><br>the format for the dates is (using today as an example):<br><br>year: 2000<br>day: 16<br>month: 8<br><br>using january 1st, 1999:<br><br>year: 1999<br>day: 1<br>month: 1<br><br>so, do this: document.write('You are '+getAge(YEAR,MONTH,DAY)+' years old');<br><br><br> <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top