4 Replies Latest reply on Oct 17, 2010 11:02 PM by jean.walsh

# Calculating years between 2 date fields

I am trying to build a form in LiveCycle Designer 7 to report missing persons.  I would like to populate a text field with the number of years between two dates entered in date/time fields; specifically using a missing person's DOB and the date they were last seen to calculate their age when last seen.

Any assistance would be greatly appreciated.

Thanks

Drew

• ###### 1. Re: Calculating years between 2 date fields

You can use either FormCalc or JavaScript for this task with LiveCycle Designer. For a beginner, I would work with FormCalc as the math is done based on the number of days.

First, date are specialaly formatted character stirngs and need some special processing before being able to use them in any computation. One uses the 'Date2Num()' function of FormCalc to extract the number of days from a given date string. One needs to be careful to make sure the date string is in the expected format. The best method is to use the 'formattedValue' of a field to get the string in the expected format. The number of days is the difference from the Epoch date. You will need to get this value for each date. Then you subtract the start date from the end date and add 1 to get the difference in days between the 2 dates. You can divide the difference in days by 365.25 to obtain the number of years and then use the 'Floor()' function to obtain the whole number or years.

• ###### 2. Re: Calculating years between 2 date fields

Thanks for the quick reply and the thoroughness of the information.  I had written almost the entire script, however the "+1" seems to have made all the difference.  It now works perfectly.

Next question: how do I get the calculated field to remain empty until the source fields are filled out?

• ###### 3. Re: Calculating years between 2 date fields

FormCalc has a 'Has Value()' function that returns 'true' only when the field has been updated, so you can add an if statement that uses the 'Has Value()' function for both of the fields and only returns a true result when both fields have data.

• ###### 4. Re: Calculating years between 2 date fields

Greetings,

I need an age in months and years.

On my LiveCycyle form I have two date fields 'dob' and 'rdtestdate'.  I am in Australia so we use dd/mm/yyyy as the format.

The field designated to display the calculated result -- 'rdage' -- is set as a calculated-read only text field.

What javascript/formcalc code would I use to calculate the age in years and months, please?

I've been studying and testing the various solutions but don't understand well enough, sorry!  When I paste in a sample and change the field names to match mine, my result remains empty.  Sometimes I get a failed script message as I try to save the form.    I've tried quite a few different scripts to no avail.

Thanks for any assistance.

jeannie