sandhya Posted January 9, 2009 Share Posted January 9, 2009 Hi all, I"ve a form in which i "ve to validate using age. i"ve a text box called 'DateOfBirth' which inputs date of birth using a pop up calender. Using that i"ve to calculate the age of the user on change. For this i'm using a java script function onChange event for the field 'DateOfBirth'. In that function i"ve taken the Date() to take present date. That function displays the date as ddd mmm dd yyyy HH:MM:ss format like Fri Jan 09 2009 14:55:21. Here i should get the date in dd mm yyyy format. How can I get it. And how can I calculate the age. Can anyone help me. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted January 9, 2009 Share Posted January 9, 2009 the Date() function returns a date object you can use the object's methods to get the specific values example var today=Date(); //the day var day=today.getDay(); //the month var month=today.getMonth(); //the year var year=today.getFullYear(); Quote Link to comment Share on other sites More sharing options...
sandhya Posted January 9, 2009 Author Share Posted January 9, 2009 Thanks for your help. It works. And How can I calculate age? By manual coding or is there any particular solution(single line) to find the time difference. If so, Can you give me that code. Thank you once again. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 9, 2009 Share Posted January 9, 2009 function getAge(bDateStr) { var today = new Date(); var tDay = today.getDate(); var tMonth = today.getMonth(); var tYear = today.getFullYear(); var bDateAry = bDateStr.split('-'); var bDay = bDateAry[0]; var bMonth = bDateAry[1]-1; var bYear = bDateAry[2]; var age = (tYear - bYear); if (bMonth>tMonth || (bMonth==tMonth && bDay>tDay)) { var age = tYear - bYear - 1; } return age; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.