How to calculate age from birthday

  • Glinniroj_support
    Asked on August 23, 2020 at 9:26 AM
  • ivettlaszlo
    Replied 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.

    1598195565datepicker1 Screenshot 10

     

    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.

    1598195837datepicker23 Screenshot 21

     

    3. Add a Form Calculation Widget and under Widget Settings enter the following code then Save:

    1598196379datepicker5 Screenshot 32

     

    I hope this helps, please let me know if you have any questions about my solution.

  • Ariel JotForm Support
    Replied 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.

    15982088222020 08 24 02 52 50 Screenshot 10

    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 = 40

    The 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

    15982094552020 08 24 03 03 51 Screenshot 21

    Using the same sample dates from above, here's the result with the new formula applied.

    15982096742020 08 24 03 06 54 Screenshot 32

    age = floor(now - dob / 30.417) / 12
    age = floor(14975 / 30.417) / 12
    age = floor (492.3233717986652) / 12
    age = 492 / 12
    age = 41

    Here 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.