Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hai,
I have a Ajax datepicker extender with a Textbox to select Date of Birth in my aspx page. I want to calculate Age when some one enter or select a date in the above mentioned textbox and should display it in a label. I googled it. But I couldnt get a perfect one. I have done it in server side. But to avoid postback I want it to be done in the client side itself. My datetime format is "dd-MMM-yyyy"..
Please help me...
Thank you.
Posted

You need to create a simple date diff function; See examples here. http://ditio.net/2010/05/02/javascript-date-difference-calculation/[^]

Read the value from the control, get the current date, and do the calc..........
 
Share this answer
 
Comments
pufo 17-Aug-11 9:12am    
Thank you for your reply.
Below javascript function will calculate age from 2 date values. Remember date values should be in DD/MM/YYYY format.

JavaScript
function CalculateAge(TodaysDate,NewDate) // Pass date in DD/MM/YYYY Format
{
   var day1 = parseInt(TodaysDate.substring(0,2),10);
   var mon1 = parseInt(TodaysDate.substring(3,5),10);
   var yr1 = parseInt(TodaysDate.substring(6,10),10);
   var day2 = parseInt(NewDate.substring(0,2),10);
   var mon2 = parseInt(NewDate.substring(3,5),10);
   var yr2 = parseInt(NewDate.substring(6,10),10);
   var diff1 = parseInt(yr1 - yr2);
   var diff2 = parseInt(mon1 - mon2);
   var diff3 = parseInt(day1 - day2);
   if(diff2 <0)
   {
   diff1 = diff1-1;
   diff2 = diff2+12;
   }
   if(diff3 <0)
   {
   diff2 = diff2-1;
   if(diff2<0)
   {
   diff1 = diff1-1;
   diff2 = diff2+12;
   }
   }
   var strAge = diff1+"."+diff2;
   return parseFloat(strAge);
}

For more details please refer this link.
 
Share this answer
 
v2

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