Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When enter date in textbox in dd/MM/yyyy format
the function for calculate age is given below.
when enter date as 12/02/2000 which is 12 feb 2000 this function calculate age.
but when the day is 26/02/2000 or day>12,not work.
function calculateAge(elt) {
bdate = new Date(elt.value);
birthMonth = bdate.getMonth();
birthDay = bdate.getDate();
birthYear = bdate.getFullYear();
todayDate = new Date();
todayYear = todayDate.getFullYear();
todayMonth = todayDate.getMonth();
todayDay = todayDate.getDate();
age = todayYear - birthYear;
alert(birthMonth);
if (todayMonth < birthMonth - 1) {
age--;
}

if (birthMonth - 1 == todayMonth && todayDay < birthDay) {
age--;
}
return age;
}
Posted

1 solution

Hi
use this javascript method to convert the input from textbox to a valid Date Object


JavaScript
bdate = GetDateObject (elt.value);


JavaScript
var GetDateObject = function ( dateString){
           
            var array = dateString.split('/');

            var day = parseInt(array[0]);
            var month = parseInt(array[1]);
            var year = parseInt(array[2]);
            var dateObject = new Date(year, month - 1, day);
            return dateObject;
             }
 
Share this answer
 
Comments
21joy 11-Dec-13 2:01am    
Thank You...
Karthik_Mahalingam 11-Dec-13 2:09am    
welcome jyoti... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900