1. Knowledge Base
  2. How-To Guides
  3. Logic, Equations, & Formulas

Calculate Age in Years from a Birthday

This article outlines the steps to calculate an individual's age based on their birthday using a date/time field and an equation field.

Use Case

Suppose you want to determine an individual's age by using their date of birth.

Requirements

This series of sets assumes that you have already set up a table to store the birthday value. You can find more information on adding tables here.

Additionally, this article utilizes fields. For more details on adding and managing fields, you can refer to this resource.

 

Steps

1.  Add a Date Field for the Birthday

For this example, we'll be using the Contact Directory sample app.  To store the value for the birthday, we'll need to add a date/time field type to a table of our preference.

We will begin by adding the date/time field to the Contact table with the following settings:

  • Name: "Birthday"

  • Required: Yes

  • Default Date: "None"

  • Time Format: "Ignore Time"

birthdayfinal

 

2.  Add an Equation Field to Calculate the Age

Next, we need to add an equation field type to calculate the age using the Birthday field we just added. 

In the same table we added the Birthday field, we'll add an equation field type with the following settings:

  • Name: "Age"

  • Equation Type: Date

  • Date Type: years

  • Result Type: Number 

  • Use the following equation in the equation editor, which subtracts the birthday from the current date: currentDate() - {Birthday}

  • Rounding: Round Down

Your equation field should look like this:

birthday

Note: Fields are processed from top to bottom in tables. Be sure to place your equation field below the date/time field that is included in the equation.

 

 

3.  Test the Equation

Now that we have added the "Birthday" field and the "Age" field to the table, it's time to test the equation. We can navigate to the Records section of the Builder and select the table we added the fields. There, we will be able to see the new fields with our Age calculation:

birthday3

 

Optional Features

Using a Conditional Equation

If you would like to make your Birthday field optional or to add an extra layer of protection against bad data, you can use the following conditional equation:

{Birthday} > -2208988800 ? currentDate() - {Birthday} : 0

Another option would be to use the following conditional equation:

{Birthday} > -2208988800 & {Birthday} != 0 ? currentDate() - {Birthday} : 0

This ensures that we can verify if there is any value stored in the Birthday field before proceeding with the calculation. If no value is present in the Birthday field, the equation will set the Age value to zero.