This post walks through how to set up a form to automatically calculate someone's age by subtracting their birth date from the current date. It involves a little bit of creative Form Builder work on the front-end, but once set up will help do all the work for you moving forward.
Step 1. Add a Date field to your form, and name it "Current Date". Let the Default Value remain as the current date, but feel free to uncheck the Mobile Visible checkbox to hide the field on mobile. It's just helping us do the calculation in the background, so hiding the field will keep things clean visually.
Step 2. Add a second Date field, and name it "Date of Birth". This time, select No Default (Blank) as the Default Value. Do leave this field as Mobile Visible.
Step 3. Add a Calculation field to your form and name it "Age in Hours". Click Edit Calculation, and set up "Current Date" as Operator 1 and "Date of Birth" as Operator 2. Select the subtraction symbol to set up the formula, and click Done. Definitely uncheck Mobile Visible to keep this field from unnecessarily cluttering your form.
Step 4. Add another Calculation field to your form and name it "Age in Days". This time when setting up the calculation, select "Age in Hours" as Operator 1, then delete Operator 2. Click New Operator, and make it a Constant. Type in "24" as the Constant, and select the division symbol. Click Done, and hide this field from mobile once again.
See what's happening here? The date calculation output is given to us in hours, so we're dividing it by 24 to get it in days. One more step, and we'll finally have it in years.
Step 5. We'll add one more Calculation field and name it "Age in Years" (whatever you'd like, really). Select "Age in Days" as Operator 1, and follow the same steps as above to add a Constant of "365.25" for Operator 2 (the .25 takes leap years into account). Select the division symbol, then click Done.
You'll want to leave this visible on mobile, and consider how many decimal places to allow. It defaults to 2 decimal places, and I recommend having at least 1 since it automatically rounds up when there are 0 decimal places allowed (for example, a 26.6 output would be considered 27 without decimal places).
Finally, save and publish your form to see it in action!
It should look nice and clean, despite all the invisible supportive fields doing calculations in the background.
Please sign in to leave a comment.