Cognito-forms – Exact age calculation in Cognito Forms (not rounded to the nearest neighbour)

cognito-forms

I used the following formula provided with modification:

Form.CampDate.Year - BirthDate.Year + (DateTime.Today.Month < BirthDate.Month or (DateTime.Today.Month = BirthDate.Month and DateTime.Today.Day < BirthDate.Day) ? -1 : 0)

I need the return result to show year and month—how do I go about it?

Example: my event date is 06 June 2016 and the birth date is 10 July 2012, so the result should be 3 years 11 month or round to age 3.5 instead of capture as age 4 year. What’s the correct Cognito Forms formula for this result?

Best Answer

Calculating Age in Cognito Forms

You can use the following calculations to determine the age of someone based on a birth date in when writing calculations in Cognito Forms. This is the official accurate way to calculate ages without rounding or mistakes due to leap years.

Age in Years

= DateTime.Today.Year - BirthDate.Year + (
if DateTime.Today.Month < BirthDate.Month or 
(DateTime.Today.Month = BirthDate.Month and 
DateTime.Today.Day < BirthDate.Day) then -1 else 0)

Age in Months

=(DateTime.Today.Year - BirthDate.Year) * 12 + (DateTime.Today.Month - BirthDate.Month) + (if DateTime.Today.Day < BirthDate.Day then -1 else 0)

Age Description

="You are " + AgeInYears.ToString("d") + " years and " + (AgeInMonths - AgeInYears * 12).ToString("d") + " months old!"

If you put the birthdate of the Statue of Liberty into these calculations, you will see that she is 129 years old today!

example of age calculation using the birth date of the Statue of Liberty

You can see these calculations in action on this shared form template: https://www.cognitoforms.com/templates/shared/CognitoSupport/AgeCalculations