Monday, May 13, 2013

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


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);
           }
    }
    }

No comments:

Post a Comment