Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How do I calculate age based on user inputed birthdate?

Avatar

Former Community Member

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 Accepted Solution

Avatar

Correct answer by
Level 5

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

View solution in original post

4 Replies

Avatar

Correct answer by
Level 5

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

Avatar

Former Community Member

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)?

Avatar

Level 5

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

$ = Round( years, 1 )

using 1 decimal place