Livecycle Designer Calculate Age From Two Dates | Current Date & DOB Date Fields

How to work out number of years between current date and date-of-birth date field in Livecycle Designer

Published by: Jamie King | Date Published: 16th April 19 | Last Modified: 16th April 19

The following Livecycle Designer example explains how to calculate the age in years between the current date and an entered date of birth. You can download and test this Livecycle PDF form yourself to understand the function of calculating an age from two date fields. The 'age from two dates' pdf form example also includes instructions and script examples of how to use Javascript and FormCalc to achieve the overall function.

While this form example shows how to calculate the age in years, between two dates, which are the current date, and a date of birth entered by the user, you can adapt the forms functions to show total number of days between two dates, or number of days lapsed between two date fields.

To download the Calculate age from two dates form click here
(Download form and open in Acrobat Reader or LiveCycle Designer)

Livecycle Designer Script To Calculate Age

The 'Age from two dates' form example uses Javascript and FormCalc over two date fields and one numeric field. The fields include 'currentDate' which is a Livecycle date field with the date format of dd/mm/yyyy. The next field is the 'dob', or date of birth field which again is a Livecycle date field with the date format of dd/mm/yyyy. The last field in the function is the numeric 'age' field. This numeric field is used to display the final calculated age, and has a display format of '1234' or specifically 'num{zzzzzzzzzzzz9}'. By having no decimal place it will round up to a whole number.

The currentDate Field - Livecycle Date Field Format

The 'currentDate' field uses Javascript on the docReady event. This will display the current date automatically when the form is opened and ready. (A handy Livecycle script to automatically display current date, if your looking for that specifically. Code below.

this.resolveNode("currentDate").rawValue = util.printd("yyyy-mm-dd", new Date());

The dob Field - Livecycle Date Field Format

The 'dob' or 'date of birth' date field uses Livecycle FormCalc on the exit event. Naturally the field will display a date picker for the user to enter a date of birth, alternatively the user can manually enter a date of birth in the correct format of dd/mm/yyyy. When the user exits this field, the FormCalc function will fire and calculate the age in years between the current date and date of birth fields, displaying it within the 'age' field. Note we divide the first answer by 365.25 for a more accurate days per year value. Code below.

form1.page.age.rawValue = Floor((Date2Num(currentDate, "YYYY-MM-DD") - Date2Num(dob, "YYYY-MM-DD")) / 365.25);

We use the FormCalc 'Floor' arithmetic function which 'Returns the largest whole number that is less than or equal to the given value'. You can learn more about the FormCalc Floor arithmetic function here in the Adobe Livecycle Help Centre.

The age Field - Livecycle Numeric Field Format

The 'age' field is a numeric field used to display our calculated age in years. It has no scripted events and requires just the display pattern to be '1234' or 'num{zzzzzzzzzzzz9}' in order to show the years calculated in whole numbers. You can of course alter this to display more specific results.

I hope this tutorial has helped you

If this tutorial has helped you, or you know a better / extended way to achieve a function to calculate age between current date and date of birth please do reach out to me or support this page by buying a coffee. Thanks.