How to calculate current age from Date of Birth using Javascript in Dynamics CRM 2011

To calculate current age from date of birth using javascript in dynamics crm 2011 follow below:

  1. Checking month difference if it is 0 simply subtract the birth year from current year.
  2. If it is negative value simply subtract the birth year from current year and -1.

Try Below Code:

function CalculateAge()
    {
    if(Xrm.Page.getAttribute("birthdate").getValue() != null) 
    { 
           var currentdate = new Date(); 
           var birthdate = Xrm.Page.getAttribute("birthdate").getValue(); 
           var monthdiff = currentdate.getMonth() - birthdate.getMonth();           
	   var currentage;

           if(monthdiff > -1) 
           {
		currentage = currentdate.getFullYear() - birthdate.getFullYear();                     
                Xrm.Page.getAttribute("new_currentage").setValue(currentage);
           }
           else
           {
		currentage = currentdate.getFullYear() - birthdate.getFullYear() - 1
                Xrm.Page.getAttribute("new_currentage").setValue(currentage);
           }
    }
    }
This entry was posted in MS CRM 2011 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.