# calculate age

## Recommended Posts

How to calculate age and save it in the submission form? I have a Date/Time field which is "Birthday", and another Text field which is "Age" in my table.

Thanks

##### Share on other sites

You can insert the following in the Footer to calculated the age, and assign the value to the "Age" field after submitting:

```<SCRIPT LANGUAGE="JavaScript">
function ageCalculate()
{
//get current date
var stamp = new Date();

//get Birthday. change "InsertRecordBirthday" if the fieldname is different
var insertDate = document.getElementById('InsertRecordBirthday').value;
var birth = new Date(insertDate);

//calculation
difference = stamp - birth;
age = Math.floor(difference / (1000 * 60 * 60 * 24 * 365.25));

//assign it to Age field
document.getElementById('InsertRecordAge').value = age;
}

document.getElementById("caspioform").onsubmit=ageCalculate;
</SCRIPT>

```
##### Share on other sites

• 2 weeks later...

Thank you for the help. I was successfully able to implement your code into my DataPage.

##### Share on other sites

• 5 years later...

Hi,

The Calculated Value form element generates dynamic calculations that automatically update as users interact with your form is now available in the current platform of Caspio. You can calculate the age of a person automatically without submitting the form and without custom programming.

You can construct the calculation using functions, logic, constants, values from other fields and SQL expressions.

You can refer to these links: https://howto.caspio.com/datapages/datapage-components/calculated-values/

Regards,

kristina

##### Share on other sites

• 6 months later...

Hi @ppbb123,

I have a formula that gets the exact age considering the leap year. You may visit this link for more details:

You may use this in your Calculated Value.

Hope this helps.

-kristina

##### Share on other sites

• 3 years later...

Hi - Just an update on this, you may also use these formulas to know the exact age by day:

```DATEDIFF(hour,[@field:Birthday],SysDateTime())/8766
```
`FLOOR(DATEDIFF(DAY, [@field:Birthday], SysDateTime()) / 365.25)`

##### Share on other sites

• 2 years later...

Hi! Caspio now offers integration with OpenAI, so you can use extensions to leverage AI to update your data based on a prompt. Here's a sample use case to get the exact age based on the provided birthday(date/time).

Request:
Calculate the age of someone born on "[@field:Birthday]" as of today.
Leave it blank if there is no value in "[@field:Birthday]"
Return only the final result as a number.

Result:

Change the 'Birthday' field to your right field name.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.