Post Closed
3770
Views
11
Comments
Calculate Age (Current date - Birth Date = Age Years)
Question

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.



2021-10-06 17-41-40
André Rodrigues

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



UserImage.jpg
Alwi Ramdan

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


UserImage.jpg
Larry Mills

Thank you Andre - I will do that.



2018-04-06 14-34-22
Tommy Hägvall

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), 


2018-11-21 17-31-38
Priya Khade

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

2018-11-21 17-31-38
Priya Khade

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

2018-07-06 11-13-55
Nathan Hobbs

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))
2020-02-23 19-49-27
Afonso Serralha

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)

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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.

2025-11-19 06-14-01
Miguel Verdasca
Champion

This is a simple way to calculate the Age, you can do a function like this:

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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.