By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

javascript to calculate the age

pradeepjain
100+
P: 563
Hii ,
I have a field where the user enters his date of birth and after this i need to calculate his/or age ..how to do it.



thanks,
Pradeep
Sep 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: 93
Please let me know the format of the of the date of birth field so that we can trace out the day,month and year separeately then after the logic is so simple.
Sep 18 '08 #2

pradeepjain
100+
P: 563
dd/mm/yyyy is the format
Sep 18 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Create a new Date object for today's date, get the input and split to get the day/month/year values and then compare. I'm sure you know how to work out someone's age from two dates.
Sep 18 '08 #4

P: 93
Expand|Select|Wrap|Line Numbers
  1. var date_of_birth=$('Id of the field where you have entered the DOB');
  2. var currentDate=new Date();
  3. var day = currentDate.getDate();
  4.   var month = currentDate.getMonth();
  5.   var year = currentDate.getFullYear();
  6.   month+=1;//As month returned starts from 0 like 0 for January
  7.  
  8. var Dob=date_of_birth.split('/');
  9. var Dob_day=Dob[0];
  10. var Dob_month=Dob[1];
  11. var Dob_year =Dob[2];
  12.  
Now you can calculate using different function I know this will be a tricky but I hope you can do because this is one of the basic programming
Sep 19 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.