# Calculate Age (Current date - Birth Date = Age Years)

Hello,

Is there any quick reference for calculating Age using a Birth Date and Current Date?  Both Date data type.

is it like this:  DiffDays(CurrDate(),BirtDate()) / 365

Appreciate any assistance.

Bye.

Hi Larry,

You can create your own server action and reuse it whenever you need it.

Also if you set function to Yes (since it will have one input and one output parameter), you can use it inside an expression  ```If( Month(BirthDate)<Month(CurrDate()),
Year(CurrDate())-Year(BirthDate) - 1,
If( Month(BirthDate)=Month(CurrDate()) and Day(CurrDate())<Day(BirthDate),
Year(CurrDate())-Year(BirthDate) - 1,
Year(CurrDate())-Year(BirthDate)))```

Cheers

Hi - tanks for this snippet - I needed to adjust the second line. Otherwise it showed one year to low

From

`Year(CurrDate())-Year(BirthDate) - 1, `

To

`Year(CurrDate())-Year(BirthDate), `

André Rodrigues wrote:

Hi Larry,

You can create your own server action and reuse it whenever you need it.

Also if you set function to Yes (since it will have one input and one output parameter), you can use it inside an expression  ```If( Month(BirthDate)<Month(CurrDate()),
Year(CurrDate())-Year(BirthDate) - 1,
If( Month(BirthDate)=Month(CurrDate()) and Day(CurrDate())<Day(BirthDate),
Year(CurrDate())-Year(BirthDate) - 1,
Year(CurrDate())-Year(BirthDate)))```

Cheers

Hi Andre,

Thanks for the snippet. But I think you have a mistake in the first line. The correct code should be:

```If(Month(CurrDate())<Month(BirthDate),
Year(CurrDate()) - Year(BirthDate) - 1,
If(Month(BirthDate)=Month(CurrDate()) and Day(CurrDate())<Day(BirthDate),
Year(CurrDate()) - Year(BirthDate) - 1,
Year(CurrDate()) - Year(BirthDate)))```

Cheers

Larry Mills wrote:

Hello,

Is there any quick reference for calculating Age using a Birth Date and Current Date?  Both Date data type.

is it like this:  DiffDays(CurrDate(),BirtDate()) / 365

Appreciate any assistance.

Bye.

Hi,

You can find function in Web Patterns named GetAge(Birthdate) where it returns the age.

Hope this is what you are looking for.

Thanks

Hi Larry Mills,

If any of the solutions worked for you then mark it as a solution so it may help others if they have any queries related to this.

Thanks

The code is still wrong, should be:

```If(Month(CurrDate())<Month(BirthDate),
Year(CurrDate()) - Year(BirthDate),
If(Month(BirthDate)=Month(CurrDate()) and Day(CurrDate())<Day(BirthDate),
Year(CurrDate()) - Year(BirthDate),
Year(CurrDate()) - Year(BirthDate) - 1))```

Hi Larry,

Although in my opinion the approach presented above is correct (as suggested by André Rodrigues with the first line correction suggested by Alwi Ramdan), I would like to offer a simpler alternative using only one condition (instead of two). This can be interesting in scenarios where performance is important, for example if you need to calculate Age as an additional attribute for an Aggregate where repetitive calculation is necessary but expensive.

The ideia here is just to compare two Integer values and decide if a person already completed its birthday at the Current Year (or any other Analysis-Date reference like "Date of Death"). The two Integer values are a composition of Day and Month where the Day will assume the 1st and 2nd digits of the Integer and the Month will assume the 3rd and 4th digits of the created value (for this reason the Month value will be multiplied by 100 so that Jan will be 100, Feb will be 200, and so on..., leaving the first two digits untouched). In practice we will have a range from 101 to 1231 (representing the limits 1.Jan and 31.Dez respectively). Note that the existing range gaps (for ex. 131 jumps to 201 or 331 jumps to 401 do not affect our analysis since we are only concerned with the boolean of a comparison and Integers maintain their scalar properties, i.e. their relative value between elements along the scale (even with gaps). This approach can be used in any Expression simply as:

If(Day(CurrDate())+Month(CurrDate())*100 < Day(BirthDate)+Month(BirthDate)*100,

Year(CurrDate())-Year(BirthDate)-1,

Year(CurrDate())-Year(BirthDate)) Regarding the domain of application ("Where" can this be used?), OS documentation for the used functions (@Date and Time section) have this "Availability Note":

» Server-side logic: Yes

» Client-side logic: Yes

» Database: Can be used with attributes in aggregates.

» Local Storage: Can be used with attributes in aggregates

Function is evaluated before the aggregate is executed (only for CurrDate())

I hope this can help!

P.S. The very same principle can be used for general DATE comparison if needed (expanding the Integer with the Year multiplied by 10k) providing the functionality of <, > and "between" (ex. use-case: search filters scenarios using Dates as parameters "Give all elemts above/below/ Date1 or between Date1 and Date2)

Hi Afonso,

Please check the date of the post you are replying to - this topic is two years old! It's a bit of a waste of time to give such a detailed reply when it's no longer needed.

EDIT: Also, the forumula is even simpler: subtract the years, and if the birthdate is less than the current date, subtract another year.

This is a simple way to calculate the Age, you can do a function like this: Miguel, I just asked Afonso to not post to such old topics, and I provided the easiest way to calculate it, and you feel the need to ignore both and post your own solution? Please don't do such things! Thanks.

I'll close the topic now before more people feel the need to add unnecesary post.