Calculating a person's age given a date of birth using rules and formulas in InfoPath
Use rules, conditions, the today() function, the number() function, and the substring() function in InfoPath to calculate a person's age from his/her date of birth.
Problem
You want to calculate a person's age given his/her date of birth in InfoPath, but you do not want to write code to do this.
Solution
Use rules, conditions, the today() function, the number() function, and the substring() function in InfoPath to calculate a person's age given his/her date of birth.
Discussion
You can accomplish this functionality as follows:
- Create a New Blank Form in InfoPath.
- Add a Date Picker control to the form template and rename it to dob.
- Add a Text Box control to the form template and rename it to age. Change its data type to Whole Number (integer) and make it Read-only.
- Add 3 rules to the dob field:
-
The Action on the first Rule must say:
Set a field's value: age = number(substring(today(), 1, 4)) - number(substring(., 1, 4)) -
The Action on the second Rule must say:
Set a field's value: age = number(substring(today(), 1, 4)) - number(substring(., 1, 4)) - 1Add the following 3 Conditions to this rule:
number(substring(xdDate:Today(), 9, 2)) < number(substring(../my:dob, 9, 2))and
number(substring(xdDate:Today(), 6, 2)) = number(substring(../my:dob, 6, 2))or
number(substring(xdDate:Today(), 6, 2)) < number(substring(../my:dob, 6, 2))Note: You will have to select The expression from the first dropdown list box in the Condition dialog box to be able to enter the expressions above.
-
The Action on the third Rule must say:
Set a field's value: age = 0Add a Condition to this rule that says:
age < 0To add the last condition, on the Condition dialog box, select age from the first drop-down list box, is less than from the second drop-down list box, and type in the number 0 in the text box.
-
The age field should now be populated with an amount of years when you fill out a date of birth in the InfoPath form.
Related InfoPath Articles:
- Calculate the difference between two date picker controls in InfoPath using rules and formulas - no code!
- Date and time basics in Microsoft Office InfoPath
- InfoPath Basics: Understanding Rules in InfoPath
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.


