Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Date and Age Fields

Avatar

Level 2

I've got a "date of birth" field on a form with a corresponding "age" field. When a user enters the date of birth, the age is automatically calculated and displayed in the age field.

This is the code on the age field.

((Date2Num(DOB.formattedValue, "MMMM D, YYYY") - Date() )/365.25)*-1

The calculation works fine.

The problem is that every time the form opens up, the figure 111 shows in the age field by default. I want it blank until the user enters a date of birth.

I've tried just about everything I know.

Help?

Also, can anyone show me the code that will allow for a calulation of the age based on the date of birth and (correct) system date, so as to give an updated age everytime the form is open.

Thanks.

Samuel

3 Replies

Avatar

Level 10

I am assuming you have written code in the calculate event of the field. Calculate event will be fired every time you open the form.

You can check for the value in the DOB feild and do the calculation only if the DOB field has a value in it.

if(DOB.rawValue ne null) then

     ((Date2Num(DOB.formattedValue, "MMMM D, YYYY") - Date() )/365.25)*-1

endif

Thanks

Srini

Avatar

Level 2

Thanks a lot.

Will you be able to help me with the second part of the question. That is..

to have the current age displayed in the age field by calculating the date of birth against the system date, so that the age will be updated year by year. ( That is if the system date is correct I guess)

Thanks Samuel

Avatar

Level 10

For your Second question:

Also, can anyone show me the code that will allow for a calulation of the age based on the date of birth and (correct) system date, so as to give an updated age everytime the form is open.

Are you having an issue with the existing code to display the correct age?

Because you are using Date() function in the Calculate code which is nothing but the current date value. And since you placed the code in the Calculate event, it will be one of the event which will be fired when the form is being open. So I think your existing code can handle it.

Thanks

Srini