Support Topics Documentation Slack YouTube Blog

Calculate a users age based on their DOB

Hey guys,

Is there a way to calculate a users age based on their Date of Birth (DOB)?

So my app is a habit tracker and based on the user’s age they will be able to select different habits to track. I’m asking users to input their DOB on registration and ideally I don’t want to then have to ask them to input their age as this will cause issues when their birthday comes up and they are a different age.

Ideally, I would like to calculate their age based on DOB and store this in the user table. Then my UI would display different habits based on their user age e.g. 13-16, 16+.

I’ve searched through the documentation and the support forum but can’t find anything related to this.

My app ID is: C209BEBE-88E7-1122-FF98-41304F96CA00

Thanks,
Eoin

Hello @Eoin_Doyle

Yes, it is possible, you can find a sample of this here Working wih Properties - Backendless SDK for JavaScript API Documentation

Regards, Vlad

Thanks for this Vlad. I’ll check it out and let you know how it get on.

Eoin

@vladimir-upirov is there a way to do this in codeless instead of using code?

Thanks,
Eoin

yes, sure

Btw, I’ve created an internal ticket to add a function curyear() to replace the 2020 value, the ticket’s number is BKNDLSS-23658

Thanks Vlad,

I’ve set this up and got the 200 success code. But it didn’t save in the age column of the user’s table. I think I need this to save in the user’s table so my frontend app (Appgyver) can read this to display the correct content for that user.

Apologies for all the questions but i’m a rookie at this coding stuff

Eoin

actually, you do not need to save the property, you can create a generated column and you will always get the age property automatically

This is awesome Vlad. I’ve got it working first time.

The generated columns feature may also answer a future question I would have had too.

Many thanks for you prompt help with this,
Eoin

My pleasure, I’m glad it works for you!

Hello, Vladimir! I’m trying to do the same thing in a generated column. And I’m curious if it’s possible to make more accurate value (not only full years but months as well)?

I tried (and failed) to transform my formula from Airtable platform that shows an age in a “years.months” format. Do any equivalents of rounding formulas exist in Backendless?

My original formula is:

CONCATENATE(ROUNDDOWN((DATETIME_DIFF(TODAY(),DOB, ‘months’)/12), 0))&"."&((DATETIME_DIFF(TODAY(),DOB, ‘months’))-(DATETIME_DIFF(TODAY(),DOB, ‘years’))*12)

Example:
DOB: August 18, 1995
age: 25.7

Also, I didn’t get how to use this ticket’s number thing in a ‘2021 - year()DOB’ formula :thinking: If a year is replaced by “curyear()”, the column doesn’t recognize the function.

Best regards,
Irina.

Hi Irina,

You would not be able to use the TODAY() function in a generated column. The system doesn’t allow non-deterministic functions. Here’s a definition of a deterministic function:

A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user

A workaround is to use a formula to request a dynamic property. For example, consider the following scenario - a table contains the dateOfBirth column:


I can request a “dynamic property” with the following formula:

year(now())-year(dateOfBirth)

This is the result I get:

To use in the data retrieval API for the Users table, it will look as follows in the REST request:

/data/Users?property=year(now())-year(dateOfBirth)%20as%20age

Hope this helps.

Regards,
Mark

1 Like

Hello Mark!
Thank you very much for the response, it provided food for thought :pray:
I’ve been thinking if this “age” value can’t be dynamic in a data table I can use some tools in a UI builder to generate and show it.

I created this form with a date and a read-only block which should calculate an age.


But I got lost in a Value Logic of the second field. (No matter what’s inside, I just can’t display it) I’m pretty sure that the result needs to be returned but it’s not possible with IF statement as I can see.

Could you give me a hint of what I’m doing wrong?
Thank you in advance!

Best regards,
Irina.

You can return value in the if statement:

image

image

If value logic hasn’t return, value of the component always will be empty.

Regards, Dima.

Little tip - use Content Logic at Text components instead of Value Logic of readonly Input

1 Like

Hi, Dima! Thank you for your advice. I switched from Input to Text and added a return. I should have known that I needed to GET that property :woman_facepalming:t2:
Now everything works and I’m completely happy!
result

Maybe someone will find this logic useful.

Regards,
Irina.