I want to automatically calculate age from birthdate when registering/updating the edit screen
Answer¶
This can be achieved with a "Server Script".
Overview¶
Enter a birthday and calculate the current age when registering or updating data. Below is a sample in which the current age calculated based on the birthday entered in the "DateA" column is inserted into "NumA".
How to use¶
- Create a "Recorded Table".
- Open the Editor tab from the Manage Table in Manage and enable "DateA" and "NumA".
- "Create a new" "Server Script" below. For "Server Script Condition", select "After formula".
Sample Code¶
JavaScript¶
function CalculateAge(birthdayVal) {
return Math.floor((ConvertDateToNum(new Date()) - ConvertDateToNum(new Date(birthdayVal))) / 10000);
}
function ConvertDateToNum (date) {
return date.getFullYear() * 10000 + (date.getMonth() + 1) * 100 + date.getDate();
}
if (utilities.InRange(model.DateA)) {
model.NumA = CalculateAge(model.DateA)
} else {
model.NumA = 0;
}