Forum Academy Marketplace Showcase Pricing Features

How calculate user age

Actually, it is.

DoB = 28/02/2000
Today = 28/02/2001

You would calculate the age as 0, whereas it is clearly their birthday today so they are 1.

DoB = 28/02/2000
Today = 28/02/2011

You would calculate the age as 10 rather than 11.

And we haven’t even tested 29th Feb :slight_smile:

2 Likes

Has anyone figured out a solution to calculating age? :smiley:

You could use toolbox and run,

function getAge(d) {
    var t = new Date();
    var b = new Date(d);
    var a = t.getFullYear() - b.getFullYear();
    var m = t.getMonth() - b.getMonth();
    if (m < 0 || (m === 0 && t.getDate() < b.getDate())) {
        a = a - 1;
    }

    return a;
}

bubble.fn.age(getAge("1/22/80"));
4 Likes

Cheeky webtask using the get-age node package.

https://wt-nigel_godfrey-gmail_com-0.sandbox.auth0-extend.com/calcage?dob=1966-10-2

var getAge = require(‘get-age’)

module.exports = function(context, cb) {
cb(null, getAge(context.query.dob));
}

1 Like

Calculate%20age%20from%20birthdate

1 Like

I’m running into a similar problem. I want to get not only the age but the months and days. Something like the MD, YM and YD units here.
I tried several options where I’m subtracting however I don’t believe it works for all corner cases.


I’m capturing the birth date from an input and transforming it into years, months, days and hours.

Probably the discrepancy starts from here with the multiplications.
Any ideas how can I get something like 32 years 5 months 17 days as an end result?

1 Like

Any luck? :roll_eyes:

It’s here buddy :wink:

image

1 Like

Fucking Genius!

Hi, has anyone come up with an accurate native-bubble solution to this? Most of the answers above don’t correctly factor in leap years. @PWC 's answer works well (if you change “bubble.fn.age” to “bubble_fn_age”), but can’t be used for a backend workflow. Ideally, I’d like my user’s birthdates to NOT be exposed to other users, just their ages, which means I need a recurring workflow to update them each day. @NigelG 's solution relies on an external service which seems to work, but since I can’t verify how long that url will work, I can’t use it as a long-term solution. @bebungbia 's solution is interesting, but does it work correctly in all cases? Seems to rely on the idea that “formatted as yyyy:converted to number” will return a fraction of the year, which I’m not sure is true.

1 Like

For those who have come across this post and have the same question that I asked a couple months ago concerning backend workflows: I found a solution. You can use the “Server Script” action from the Toolbox plugin in a backend workflow and retrieve the age value directly from it. I used the following modified code from @PWC 's answer, which I believe was in turn modified from a decade-old stack overflow thread:

var t = new Date();
var b = new Date(data);
var a = t.getFullYear() - b.getFullYear();
var m = t.getMonth() - b.getMonth();
if (m < 0 || (m === 0 && t.getDate() < b.getDate())) {
    a = a - 1;
}

a;

And then I set the Data field to:

user's birthdate:formatted as MM/DD/YY

Hope this helps!

2 Likes

Remarkably it is still working :slight_smile:

But I may well use the Toolbox as I want to return age in months.

1 Like

Doesn’t actually work.

The actual remarkable thing here, is that I managed to get some code to run years ago, and it still works. And then someone comes along, is critical, and then provides a solution that doesn’t work.

FML

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.

1 Like

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:

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

1 Like

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.

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 :