locked
Date Calculation RRS feed

  • Question

  • I have two fields on a custom form. One is a date of birth field and the 
    other is an age field. I would like the age field auto populated when the 
    date of birth field is entered.
    
    I tried the code below but I think this is for CRM 3 only. It doesn't seem 
    to work in CRM 4? Unless I am doing something wrong? My age field is just an 
    "nvarchar field" so maybe that is where I am going worng?
    
    //AGE CALCULATION 
    if(crmForm.all.new_dob.DataValue != null)
    {
           var now = new Date();
           var birthday = crmForm.all.new_dob.DataValue;
           var monthdif = now.getMonth() - birthday.getMonth();
           var age = crmForm.all.new_age;
           if (monthdif > -1)
           {
                   age.DataValue = now.getFullYear() - birthday.getFullYear();
           }
           else  if (monthdif < 0)
           {
                   age.DataValue = now.getFullYear() - birthday.getFullYear()-1;
           }
    }
    Tuesday, January 26, 2010 3:22 PM

Answers

  • try following code..

    //AGE CALCULATION 
    if(crmForm.all.new_dob.DataValue != null)
    {
           var now = new Date();
           var birthday = crmForm.all.new_dob.DataValue;
           var monthdif = now.getMonth() - birthday.getMonth();
           
            var ageVar = null;
           if (monthdif > -1)
           {
                   ageVar= now.getFullYear() - birthday.getFullYear();
           }
           else  if (monthdif < 0)
           {
                   ageVar= now.getFullYear() - birthday.getFullYear()-1;
           }
    
           //alert(ageVar);
           crmForm.all.new_age.DataValue = ageVar;
    }
    Tuesday, January 26, 2010 3:26 PM
    Answerer