-
Glinniroj_supportAsked on August 23, 2020 at 9:26 AM
-
ivettlaszloReplied on August 23, 2020 at 11:22 AM
Hi,
You can calculate an age by birthday either using this form: https://www.jotform.com/build/202354994345360
or by adding to your existing form the following Form Elements and a Form Calculation widget:
1. Add a Date Picker and then set the Default Date to "CURRENT". You can either hide or show this field - if you decide to hide it (recommended), you can do that under the Advanced option.
2. Add a second Date Picker, and leave the Default Date at "NONE" and under the "Limits" you can set it to only accept past dates, given that it's going to be a birthday.
3. Add a Form Calculation Widget and under Widget Settings enter the following code then Save:
I hope this helps, please let me know if you have any questions about my solution.
-
Ariel JotForm SupportReplied on August 23, 2020 at 3:43 PM
Hello,
Thank you for your message.
A little correction on the formula. Instead of dividing the number of days by year(365.25), I suggest you divide it by month(30.417) instead, apply floor function(omit decimal part) to the quotient, then divide the result by 12.
Here's one example where dividing the number of days by 365.25 could get a wrong age value. The age in the following example should be 41, but we get 40 instead.
Here's why. The number of days between 09/10/1980 and 09/10/2021 is 14975.
Using the formula age = floor((now - dob) /365.25)...
age = floor((14975) / 365.25)
age = floor(40.99931553730322)
age = 40The result is 40, which is incorrect. It should be 41.
So I suggest you use the following formula instead:
age = floor(now - dob / 30.417) / 12
Using the same sample dates from above, here's the result with the new formula applied.
age = floor(now - dob / 30.417) / 12
age = floor(14975 / 30.417) / 12
age = floor (492.3233717986652) / 12
age = 492 / 12
age = 41Here a demo form that you can clone if you want to see how it works from the inside:
Hope that helps. Let us know if you have any questions or if you need further assistance.
Related Guide: How to Perform Form Calculation Using a Widget.