Forum Documentation Showcase Pricing Learn more

How calculate user age

How might I calculate the age of a user? I have a fiel in the “Member” data type called “Años”, it is date type. Thank you very much.

The age of a user is not a date, it’s a number. You can catch the birthday, then subtract their birthday from the current date. Start exploring down that road, and see what you come up with.

Yup. I was thinking about that. But, when i’m going to subtract the birthday from the current date i must format it.

I have a datediff example on my demo app;

editor:

Date differences won’t work all that well due to leap years. So you can’t, for example, divide the difference in seconds by 60 * 60 * 24 * 265 and round it, it will be days out.

So you probably need to do the years first, and then work out if their birthday in this year is past and add 1 if birthday month > current month OR birthday month = current month and birthday day > current day. Or something like that.

Yup. Actually, it is a bit difficult.

It’s actually not all that difficult. In mine, I have a field called DoB. The formula I used is Current date/time - Input DoB’s value:format as days / 365.255:floor.

It works great.

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:

1 Like

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"));
3 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