> For the complete documentation index, see [llms.txt](https://docs.agilecase.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.agilecase.com/developer-documentation/custom-form-scripting/calculating-age-from-date-of-birth.md).

# Calculating Age From Date of Birth

This example shows how to use a JavaScript field to calculate a client's age from their date of birth.

## Script

### Calculating Age

```javascript
// The first field specified is the one on which the Javascript field shall depend on.
var inputDate = $("#CustomField_6040").val();
    var today = new Date();
// This line controls the formatting of the data output in the JavaScript Field
    var birthDate = new Date(inputDate.split('/')[2],inputDate.split('/')[1]-1,inputDate.split('/')[0]);
// The script below pull out the clients date of birth and the current date in order to calculate their age.
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) 
    {
        age--;
    }
// This is the ID of the new JavaScript field
 $("#CustomField_6039").html(age);
```

## Age Calculated from Date of Birth

<figure><img src="/files/PMQlUTwoMxPA3ttqhYTf" alt=""><figcaption></figcaption></figure>

This screenshot shows an age calculated from the date of birth above.

## New Age Calculated from Changed Date of Birth

<figure><img src="/files/x8j7nePUROwsYF6eqoSy" alt=""><figcaption></figcaption></figure>

We can see the JavaScript field in action if the date of birth is changed. The age will be automatically updated to reflect the new date of birth.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.agilecase.com/developer-documentation/custom-form-scripting/calculating-age-from-date-of-birth.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
