How calculate user age

First off, I didn’t mean to disparage your response, and apologize if that’s how you interpreted it. My intention was to acknowledge that your response actually does work in edge cases, and to provide an alternative for those who don’t want to rely on external sources which we don’t have control over.

In regards to the code not working, I was able to test it out just now in an internal app, and it seems to work fine on my end. I’d love to share a link to an editor to demonstrate this, but unfortunately backend workflows require a paid plan.

If you let me know what issue you’re having, I’d be happy to help you debug it. Your use case of months should be possible with slight modifications.

I’m also working on a plugin which will have a number of useful datetime-related utilities including get age, so I can update this thread when I’ve finished it.

I posted that in 2018 and never really expected anyone to use the function. Am amazed it is still working even though webtask had been discontinued.

Suspect the issue is with the format of what is being passed in.

MM/DD/YY as a format as ? Are you sure ?

I will answer my own question …

“formatted as MM/DD/YY” looks like it wants to be a Custom format (MM/DD/YY) which doesn’t work). My fault for overcomplicating it.

But actually, you want YYYY because people are living to 100 now :slight_smile:

So…Just pick the 2nd on the list.

Anyway, months in my case was as simple as the difference between the two dates in days / 31 as it just needs to be approx.

2 Likes

Actually, you (simply) need to do what you do in real life:

  • you look at the year of birth and make the difference with the current year
  • then you check whether the current day of the year is higher than the birth day of the year, and decide to subtract 1 or not

Here is the formula (i’m using a date / time picker):

image

Hope it helps someone! :slight_smile:

1 Like

There’s an easy way:

“Current time:extract date >= BIRTHDAY:extract date:formatted as text”
if yes → Current time:extract year - BIRTHDAY:extract year
if not → Current time:extract year - BIRTHDAY:extract year - 1

if you need as number, convert it as number later

3 Likes

I have found a way which is intuitive and easy to write in native bubble. It handles leap years perfectly as far as I can tell.

  1. subtract DOB year from this year
  2. if current date in DOB’s year is before DOB, subtract 1, otherwise subtract 0

image

More details:

And it works in the back end too.

7 Likes

I also had to determine somebody’s age depending on a field called “Date de naissance” (date of birth in french) that I wanted to compare to the current date and then store the result in an another field called “Age”.

Here is how I did it, i works fine, at least fo me :wink:

  1. On the “Age” field, Appearence - Content format = Integer
  2. On the “Age” field, Appearence - Initial content :
    image
  3. On the “Age” field, Conditional, added those 3 conditions :
    image
    image
    image

And that’s all.

To explain how it works, as there is no predefined method in Bubble to handle this “out of the box”, I implemented the following logic :

1 Like

Great solution Michael! Works perfectly. Thank you!

divide by 365.25 and round

You can use current date extract year - date pickers value extract year, then condition show only visible when current date extract date < date pickers value extract date , then text value would be current date extract year - date pickers value extract year -1

Works for me :smile:

Hi, this is how I calculate a Users age.

You can substitute a field such as userAge instead of the DatePicker. It works the same. Seems to work on Leap Years too.

Hope this helps