To calculate current age from date of birth using javascript in dynamics crm 2011 follow below:
- Checking month difference if it is 0 simply subtract the birth year from current year.
- 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); } } }