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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to return age (in years) is not working properly 1

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
US
I have the following function:
Code:
function ageInYears(bDay)
{
   //based on:  [URL unfurl="true"]http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20664053.html[/URL]
   now = new Date()
   bD = bDay.split('/');
   if(bD.length==3)
   {
      born = new Date(bD[2], bD[1]*1-1, bD[0]);
      years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
      return years;
   }
}
to which I'm passing a date, for example: 10/1/1989 The problem is that it returns the same age regardless of whether the "person" has his/her birthday this year already or not.

For example, if today (10/2/2009) I want to check the age of two people: one born on 10/1/1989 and another born on 10/3/1989, the result "20" is returned for both.

What's wrong with this function?

Thanks much,
Eliezer
 
I think its Date(year, month, day) and January is 0, December is 11. Try
Code:
born = new Date(bD[2],(bD[0])-1,bD[1]);

-----------------------------------------
I cannot be bought. Find leasing information at
 
Nothing, your input is what's wrong.

The function expects day/month/year not month/day/year

So basically what you are saying is a person who was born on February Ten is twenty years old and a person born in March ten is also 20 years old.

Which is correct.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Phil, you are 100% correct. But how do I change the expected input to month/day/year ?

Thanks,
Eliezer
 
Jaxtell, I misunderstood what you wrote in your first post. I now tried it and it works perfectly!

Thanks [star]!!!
Eliezer
 
Yeah, apparently I lack the skill of effective communication. I'm glad it worked for you.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Eleizer,

Please don't tell me you paid to get a subscription on "Experts Exchange" but ended up solving your solutions with the "Free Experts" here :)

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
No, I will not tell you that. I have never signed up for Experts Exchange, but for some reason am always able to see the "Accepted Solutions" by scrolling a-l-l the way down on their site.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top