These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Creating a field to calculate Age

lg150jls
Registered: Jun 30 2008
Posts: 9

I have created a form in LiveCycle and want it to calculate a persons age as of a certain date once they enter their birthdate. How do I do this?

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Using FormCalcs "Date2Num()" function to get the number days from the Epoch date for the current date the DOB and the difference can be divided by 365 and turnccated to the years.

Or you can use the JavaScript DateTime object to the various date properties and perform the appropriate computations

George Kaiser

lg150jls
Registered: Jun 30 2008
Posts: 9
My FN button does not seem to do anything any suggestions
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You have to supply the required parameters. You can also type the code in the appropriate action.

Use of search turned up [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=587]age from date of birth.[/url] There is a sample for the number of year with decimal and the truncated number of years.

George Kaiser

lg150jls
Registered: Jun 30 2008
Posts: 9
I used this formula
Floor( (Date2Num(DOB.formattedValue, "MM D, YYYY") - Date2Num(Regdate.formattedValue, "MM D, YYYY")) / 365.25)

However it keeps coming back to 0, it is not changing when the Birthdate is entered.
KERPAR
Registered: Feb 2 2010
Posts: 5
Hello,

I have figured out how to calculate age but I am trying to get the number to return not in a whole number. For instance if someone is turning 40 tomarrow I need to know this as it could change there benefit elidgability. I have tried everything including the format of the field being a decimal instead of integer etc. Any help would be greatly appriciated!!! Below is my current formula.

Thanks,
Karen


Floor( ( (Date2Num(DateTimeField1[1].formattedValue, "MMM D, YYYY") - Date2Num(DateTimeField4[0].formattedValue, "MMM D, YYYY") ) / 365.25 ) )
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The 'Floor()' function Returns the largest whole number that is less than or equal to the given value. This truncates the returned value at the decimal point.

I do not think that removing the Floor function alone will give you the result you want. You might want to compute the age with the dates and compare that result to the computation of the date with the end date increased by 1 day. Because you want to know the age of the individual as of the next day.

George Kaiser

KERPAR
Registered: Feb 2 2010
Posts: 5
Thank you I will try that, so I should basically add -1 after my whole formula?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can restate the calculation script to more clearly see what is being done:
  // compute age// get today's date in daysvar vEndDate = Date2Num(DateTimeField1[1].formattedValue, "MMM D, YYYY")// get DOB date in daysvar vStartDate = Date2Num(DateTimeField4[0].formattedValue, "MMM D, YYYY")// compute the difference in days age in daysvar vAgeDays = vEndDate - vStartDate// convert to whole yearsvar vAgeYears = Floor(vDiffDays / 365.25) // compute age for next dayvar vTestDay = vEndDate + 1// age in days for next dayvar vTestAge = vTestDay - vStartDate// age for next day in whole yearsvar vTestYears = Floor(vTestAge / 365.25)// compare ages in yearsif(vTestYears <> vAgeYears) then// some actionendif

George Kaiser

KERPAR
Registered: Feb 2 2010
Posts: 5
I will try this, thank you so much for all your help!!!!!! :D
jean299
Registered: Nov 15 2009
Posts: 19
Nearly there! I sucessfully used a simple little formula for calculating age based on two date fields. I got it working as follows (with Australian date format):
----------

if (HasValue(dob)) then
if (HasValue(rdtestdate)) then
var rdtestdate_ = Date2Num(rdtestdate.formattedValue, "DD/MM/YYYY")
var dob_ = Date2Num(dob.formattedValue, "DD/MM/YYYY")
var diff = rdtestdate_ - dob_
$.rawValue = Floor(diff / 365.25)
else
$.rawValue = null
endif
else
$.rawValue = null
endif
-----------------------

What I need is to report the results in terms of years and months rather than round to years only.

I can get the years as a decimal by replacing the Floor function with --
Round(diff / 365.25,2)
-- but we actually need the result to show as years and months,

Can ayone assist with formatting as years and months?

Many thanks for your assistance!

jeannie

Novice Niki
Registered: Aug 26 2011
Posts: 1
Jean have you discovered how to get the years and months from the birthdate?

Nichole Syme

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If you look at the Num2Date function and the formatting options, you can get many parts of the date by the way you format provide.

// get date string
var cDate = DateTimeField1.formattedValue
// number of days from Epoch date
var nDate = Date2Num(cDate, "MMM DD, YYYY")
// 4 digit year
var nYear = Num2Date(nDate, "YYYY")
// month number
var nMonth = Num2Date(nDate, "M")
// long month
var cMonth = Num2Date(nDate, "MMMM")
// date
var nDate = Num2Date(nDate, "D");
// display values
var cMsg = Concat("Sttrig date: ", cDate, "\u000d")
cMsg = Concat(cMsg, "Date number: ", nDate, "\u000d")
cMsg = Concat(cMsg, "Year: ", nYear, "\u000d")
cMsg = Concat(cMsg, "Month number: ", nMonth, "\u000d")
cMsg = Concat(cMsg, "Long month: ", cMonth, "\u000d")
cMsg = Concat(cMsg, "Date: ", nDate, "\u000d")
xfa.host.messageBox(cMsg)

You can also convert the string date value to JavaScript's date object and from there you get various parts of the date object using the various methods of the date object. These include the full 4 digit year, month (zero based), date, day of the week (zero based).

George Kaiser

jean299
Registered: Nov 15 2009
Posts: 19
Hi Niki,

Yes I have been using this in our forms at work. Below is the instructions I put together for others to be able to do the same thing. Unfortunately, the red and green text don't show in this forum post, so I hope you can work it out. The instructions are for someone who needs step by step guidance. If you just need the formulas, you can scroll down to copy them and simply replace the field names with your own.

To calculate a chronological age in years and months given two date fields requires the creation of a calculated field with a script that works out the problem for you. There are a lot of complex scripts available for working with dates. Depending on how precise your information needs to be, you may want to seek out the script that best suits your needs. This example provides a very simple script that is precise enough for working out the chronological ages of children given their birthdate and another date, e.g. the date of a reading test.

Assuming you have already created two date fields to be used as references in your calculations, the first thing to do is to create two text fields (for years and months) and make them both Calculated - Read Only using the Value tab.

Take note of where the new fields are located in the Hierarchy, because if they are in a different subform or on a different page from the date fields being referenced, you have to use the full paths in the formula instead of just the field names.


Second, select each field in turn and paste in the following scripts for the fields.

Use the Window menu and select Script Editor to open the area where scripts can be typed or pasted.
Select Calculate from the dropdown menu labelled 'Show' to open the specific section where calculation scripts are entered.

From the 'Language' dropdown menu select FormCalc, because the script being used here is in that language.

NB:

The text in red below shows the form field names. You need to substitute these with your own form field names for your date fields. (The oldest date goes where the dob field is referenced, and the most recent date goes where the testdate field is referenced. Also, if the fields are not in the same subform or on the same page, you have to put in the full path, e.g., instead of simply dob (for date of birth) you might put Page1.studentdetails.dob.

The text in green below is whatever short text string you want to label the variables used in the calculation. An example of labelling variables used in the scripts below is -- The part of the script that converts the date of birth into a computer generated number representing a number of days is called dob_. For simplicity, the field name with an underscore was used to label this variable, but any other text string could have been used, like dobindays.


Here is the FormCalc code used:


for the year field

---------------------------------------------


if (HasValue(dob)) then
if (HasValue(testdate)) then
var testdate_ = Date2Num(testdate.formattedValue, "DD/MM/YYYY")
var dob_ = Date2Num(dob.formattedValue, "DD/MM/YYYY")
var diff = testdate_ - dob_
$.rawValue = Floor((diff /7)/(52))
else
$.rawValue = null
endif
else
$.rawValue = null
endif


---------------------------------------------


for the month field


---------------------------------------------


if (HasValue(dob)) then
if (HasValue(testdate)) then
var testdate_ = Date2Num(testdate.formattedValue, "DD/MM/YYYY")
var dob_ = Date2Num(dob.formattedValue, "DD/MM/YYYY")
var diff = testdate_ - dob_
var nyears = Floor((diff/7)/(52))
$.rawValue = Floor((((diff/7)/52) - nyears)*12)
else
$.rawValue = null
endif
else
$.rawValue = null
endif


----------------------------------------------