Can users' ages be calculated?
While it is not possible to use kintone to automatically calculate an age using the current date as a base, you can calculate an age based on the date a record was updated or a specific date.
An age is calculated by finding the difference between a date of birth and another value. For the other value, use an "Updated datetime" field if you want to calculate an age based on a record's updated datetime, or use a "Date" or "Date and time" field if you want to calculate an age based on a specific date or datetime.
Here, we will describe two methods for calculating an age based on a record's updated datetime: a simplified method that calculates an approximate age without taking leap years into consideration, and a method that calculates an age while taking leap years into consideration.
- Method 1: Age calculation that does not take leap years into consideration
- Method 2: Age calculation that takes leap years into consideration
Method 1: Age calculation that does not take leap years into consideration
With this method, leap years are not taken into consideration and a year is considered to be 365 days. An age is calculated as the number of years from a date of birth to an updated datetime. The calculated age may be higher than the actual age.
Preparation
Place the following fields on the app form in App Settings, and set their field names and field codes.
-
"Updated datetime" field
- Field name: Updated Datetime
- Field code: Updated_Datetime
-
"Date" field
- Field name: Date of Birth
- Field code: Date_of_Birth
-
"Calculated" field
- Field name: Age
- Field code: Age
Setting up the formula
Set the following formula in the "Calculated" field.
Specify the Number of Decimal Places to Display in the "Calculated" field as 0.
-
The "Age" field
(Updated_Datetime-Date_of_Birth)/ (60*60*24*365)
Formula explanation
The number of years between the updated datetime and the date of birth is calculated, with one year being equal to 365 days.
For information on functions and how date values are treated, refer to the following page.
Calculation of Date, Time, and Date and Time
Method 2: Age calculation that takes leap years into consideration
This method calculates an age as the number of years from a date of birth to an updated datetime, while taking into consideration leap years.
Preparation
Place the following fields on the app form in App Settings, and set their field names and field codes.
-
"Updated datetime" field
- Field name: Updated Datetime
- Field code: Updated_Datetime
-
"Calculated" field
- Field name: Updated Datetime A
- Field code: Updated_Datetime_A
-
"Date" field
- Field name: Date of Birth
- Field code: Date_of_Birth
-
"Calculated" field
- Field name: Date of Birth A
- Field code: Date_of_Birth_A
-
"Calculated" field
- Field name: Age
- Field code: Age
Setting up the formula
Set the following formula in the "Calculated" field.
-
The "Updated Datetime A" field
DATE_FORMAT(Updated_Datetime,"YYYYMMdd","system")
-
The "Date of Birth A" field
DATE_FORMAT(Date_of_Birth,"YYYYMMdd","system")
-
The "Age" field
ROUNDDOWN((Updated_Datetime_A-Date_of_Birth_A)/10000,0)
Formula explanation
The updated datetime and date of birth are converted to values in a date and time format using the DATE_FORMAT function, and then the difference between the two values is calculated.
Since the age is represented from the ten-thousands place of the calculation result, the calculation result is divided by 10,000, and then the resulting number is rounded down to the nearest whole number using the ROUNDDOWN function.
For information on the functions used, refer to the following pages.
DATE_FORMAT Function: Specifying the Display Format of Date and Time
ROUND, ROUNDUP, and ROUNDDOWN Functions: Round Off, Round Up, and Round Down Numeric Values