I happened to need exactly this functionality in a form I am developing this morning! The code below is a straight cut and paste from my solution. Which I am confident is working. If you are still getting an error it could be something else. This code works on "contact". As the OOB field name for date of birth on the contact is "birthdate". If you are using another entity or have a custom field for date of birth you may need to substitute that. Hope this helps. function onLoad() { BirthDateFunction(); } function onChange_birthdate() { BirthDateFunction(); } function BirthDateFunction() { var birthDate = Xrm.Page.getAttribute("birthdate").getValue(); var birthDateControl = Xrm.Page.ui.controls.get("birthdate"); if(birthDate != "" && birthDate != null) { var age = CalculateAge(birthDate); var newLabel = "Date Of Birth (" + age + ")"; } else { var newLabel = "Date Of Birth"; } birthDateControl.setLabel(newLabel); } function CalculateAge(birthDate) { birthDate = new Date(birthDate); var todayDate = new Date(); var years = (todayDate.getFullYear() - birthDate.getFullYear()); if (todayDate.getMonth() < birthDate.getMonth() || todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() < birthDate.getDate()) { years--; } if(todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() == birthDate.getDate()) { alert("Say happy birthday!"); } return years; }
↧