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
Has anyone figured out a solution to calculating age?
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"));
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));
}
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?
It’s here buddy
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.
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!
Remarkably it is still working
But I may well use the Toolbox as I want to return age in months.
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