548
Views
13
Comments
Solved
Logic for calculating age
Question

How do you spell a condition to find the correct age?

I want the age to correspond to 26 years because I have 1996 as an attribute in the structure

2021-04-17 09-53-48
José Gonçalves
Solution

Hi Sabrina,

check this post, you'll get the expression to calculate:

https://www.outsystems.com/forums/discussion/35960/calculate-age-current-date-birth-date-age-years/

Regards

2022-10-24 14-53-08
Sabrina Avolio

i need an if condition to calculate age

2021-04-17 09-53-48
José Gonçalves

The condition is full of IFs:


If(Month(CurrDate())

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

      If(Month(BirthDate)=Month(CurrDate()) and Day(CurrDate())

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

            Year(CurrDate()) - Year(BirthDate)))

2023-03-16 16-29-51
Paulo Rosário

Good formula! It should do the trick easily 

2022-09-27 05-23-26
Mohammad Shoeb

Hi Sabrino,

You can use the year different by using the inbuilt action in Outsystems.

Year(CurrDate()) - Year("Yourdate").

if you want the date in human readable format the you can use FormatDate() Action.

you can also use pretty date format component available on forge.

Hope this helps,


Regards,

Shoeb


2021-04-17 09-53-48
José Gonçalves

This will assume the person's birthday already happened this year.

2022-10-24 14-53-08
Sabrina Avolio

Help

Senza titolo.png
2021-04-17 09-53-48
José Gonçalves


I already wrote the formula needed to calculate the exact age.

2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi All,

In that post Jose is pointing to, I'd like to direct attention to the answer that came 2 years later, from Afonso Seralha.  (At least if age is wanted in terms of how many birthdays have passed.)

Age =  Year(TestDate)- Year(Birthdate) -

If(Month(TestDate)*100+Day(TestDate)<Month(Birthdate)*100+Day(Birthdate),1,0)

Dorine

PS @José Gonçalves : i notice that when I paste text in this forum, it cuts off everything after a < sign that isn't surrounded by spaces, I suspect that also happened to your answer.

2021-09-06 15-09-53
Dorine Boudry
 
MVP

Alternatively,

If you are not interested in the age itself, but just want to check if somebody has reached a treshold age of let's say 21,

I guess you can just have the condition :

AddYears(BirthDate,21) <= CurrDate()

2023-10-21 19-42-11
Tousif Khan
Champion

Hello

You can also simply write a JavaScript program to calculate Age

You can write it in a client action and make it as a function and you can reuse it anywhere, it will return you Age

You can check demo Sample here - 

https://personal-wwkmlamd.outsystemscloud.com/AgeCalculator/CAlculateAge?_ts=638008671306924614

you can also refer to other solution in above comments

I have attach Oml for reference

I hope this helps

Best Regards

Tousif Khan

AgeCalculator_Sample.oml
2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi @Tousif Khan ,

this will be upping the age 1 day too early on some occasions involving leap years.  

Can't really explain it, but you can try by adjusting your app to take in a second date instead of using CurrDate(), and then test with for example age on 31/12/2021 for someone born on 01/01/2020.  It should still be 1, but it is already 2.  On 31/12/2025 it should still be 5, but it is 6.

also, doing it in Javascript limits the applicability to client side.

Dorine

UserImage.jpg
Sayed Husain Shehab

Hi @Sabrina Avolio 

I know I might be a little late, but you can use the following logic:

(TextToInteger(FormatDateTime(CurrDate(),"yyyyMMdd")) - TextToInteger(FormatDateTime(Birthdate,"yyyyMMdd"))) / 10000


Husain

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.