4 Replies Latest reply on Mar 28, 2012 7:34 AM by dohanlon

# How do I calculate age based on user inputed birthdate?

Hi there -

I'm working on a form where the user will enter both their date of birth and their start date for employment. I need to create two fields that will calculate their full years of age based on their date of birth, and their full number of years of service, based on their start date.

Does anyone have a good script that will make these calculations? I normally use FormCalc, but am open to Java if there's an easier solution.

Thanks,

Erin

• ###### 1. Re: How do I calculate age based on user inputed birthdate?

If you are using date fields, then on the calculate of the text fields, you can put something like

```if ( not DateTimeField1.isNull ) then
var dateofBirth = Date2Num( DateTimeField1, "YYYY-MM-DD" )
var currentDate = Date()

var years = ( currentDate - dateofBirth ) / 365
\$ = Floor( years )
else
\$ = ""
endif
```

will calc the age, while something similar for startdate

```if ( not DateTimeField2.isNull ) then
var startDate = Date2Num( DateTimeField2, "YYYY-MM-DD" )
var currentDate = Date()

var years = ( currentDate - startDate ) / 365
\$ = Floor( years )
else
\$ = ""
endif
```
• ###### 2. Re: How do I calculate age based on user inputed birthdate?

Thank you so much! That worked perfectly.

• ###### 3. Re: How do I calculate age based on user inputed birthdate?

Would it be possible to change the calculation for the Years of Employment to calculate years and months? For example, if the input is 2/26/2011, the output would be 1.1 (1 year, 1 month)?

• ###### 4. Re: How do I calculate age based on user inputed birthdate?

You could use Round function instead of Floor in the script, so something like

\$ = Round( years, 1 )

using 1 decimal place