Display calculated age on NewForm/EditForm/DispForm when supplied with D.O.B

Home Forums Classic DFFS Display calculated age on NewForm/EditForm/DispForm when supplied with D.O.B

Viewing 1 reply thread
  • Author
    Posts
    • #8687

      Hello – we have a form that staff fill out online – they ask for the applicants date of birth as part of the process – I would like to be able to have the form calculate the person’s age in years below once the DOB field has been filled…

      I am hoping that there is a way to do this within DFFS as I cannot display a calculated field in the newform – where I need it the most.

    • #8692
      Alexander Bautz
      Keymaster

        Hi,
        This code example will pull the date from a field named “Birthday”, and write it to a field named “Age”. Please note that this will be a static value, and will not automatically tick up a notch next year without you editing the list item.

        Put this in the Custom JS:

        function calculateAge(){
        	var dfo, sDate, now, age;
        	dfo = spjs.dffs.getRegionalSettings().DateFormat;
        	sDate = spjs.dffs.strToDateObj("Birthday",dfo);
        	now = new Date();
        	age = now.getFullYear() - sDate.getFullYear();
        	if(now.getMonth() < sDate.getMonth()){
        		age -= 1;
        	}else if(now.getMonth() === sDate.getMonth()){
        		if(now.getDate() < sDate.getDate()){
        			age -= 1;
        		}
        	}
        	setFieldValue("Age",age);
        }

        Then call this function from a rule in DFFS by putting the function name in the “Run these functions / trigger these rules” field.

        Let me know how it works out.

        Alexander

    Viewing 1 reply thread
    • You must be logged in to reply to this topic.