# How calculate user age

**URL:** <https://forum.bubble.io/t/how-calculate-user-age/9038>\
**Category:** Questions\
**Created:** [December 26, 2016, 4:39pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038 "2016-12-26T16:39:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilsito1999](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wilsito1999](https://forum.bubble.io/u/wilsito1999)\
**Post date:** [December 26, 2016, 4:39pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/1 "2016-12-26T16:39:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andrewgassen](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/andrewgassen/32/44248_2.png) [@andrewgassen](https://forum.bubble.io/u/andrewgassen)\
**Post date:** [December 26, 2016, 4:44pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/2 "2016-12-26T16:44:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wilsito1999](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wilsito1999](https://forum.bubble.io/u/wilsito1999)\
**Post date:** [December 26, 2016, 5:17pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/3 "2016-12-26T17:17:57Z")

</div>

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

 ![](https://us1.discourse-cdn.com/bubble/original/2X/f/fc4b2335af1ef40e98435af2dc36028531bc53c8.png)

---

<div class="post-metadata">

**Author:** ![anon39705632](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@anon39705632](https://forum.bubble.io/u/anon39705632)\
**Post date:** [December 26, 2016, 7:34pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/4 "2016-12-26T19:34:32Z")

</div>

I have a datediff example on my demo app;

> **[Bubble - Visual Programming](https://dave-auld-demo-stuff.bubbleapps.io/version-test/timezone?debug_mode=true)**
>
> Build stuff without code and launch a startup without a tech-cofounder! Bubble is a visual programing language. Instead of typing code, use a visual editor to build applications.

editor:

> **[Bubble Editor - dave-auld-demo-stuff](https://bubble.io/page?type=page&name=timezone&id=dave-auld-demo-stuff&tab=tabs-1)**
>
> Build stuff without code and launch a startup without a tech-cofounder! Bubble is a visual programing language. Instead of typing code, use a visual editor to build applications.

---

<div class="post-metadata">

**Author:** ![NigelG](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@NigelG](https://forum.bubble.io/u/NigelG)\
**Post date:** [December 26, 2016, 8:13pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/5 "2016-12-26T20:13:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wilsito1999](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wilsito1999](https://forum.bubble.io/u/wilsito1999)\
**Post date:** [December 26, 2016, 8:16pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/7 "2016-12-26T20:16:02Z")

</div>

Yup. Actually, it is a bit difficult.

---

<div class="post-metadata">

**Author:** ![david6](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@david6](https://forum.bubble.io/u/david6)\
**Post date:** [April 3, 2017, 9:24pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/8 "2017-04-03T21:24:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NigelG](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@NigelG](https://forum.bubble.io/u/NigelG)\
**Post date:** [April 4, 2017, 7:04am UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/9 "2017-04-04T07:04:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jamie](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jamie/32/23275_2.png) [@jamie](https://forum.bubble.io/u/jamie)\
**Post date:** [December 20, 2018, 9:57pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/10 "2018-12-20T21:57:02Z")

</div>

Has anyone figured out a solution to calculating age? 😃

---

<div class="post-metadata">

**Author:** ![PWC](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/pwc/32/29669_2.png) [@PWC](https://forum.bubble.io/u/PWC)\
**Post date:** [December 20, 2018, 10:40pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/11 "2018-12-20T22:40:44Z")

</div>

You could use toolbox and run,

```auto
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"));
```

---

<div class="post-metadata">

**Author:** ![NigelG](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@NigelG](https://forum.bubble.io/u/NigelG)\
**Post date:** [December 21, 2018, 5:41pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/12 "2018-12-21T17:41:17Z")

</div>

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));  
}

---

<div class="post-metadata">

**Author:** ![ronelo.gomez](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@ronelo.gomez](https://forum.bubble.io/u/ronelo.gomez)\
**Post date:** [October 8, 2019, 2:06pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/13 "2019-10-08T14:06:17Z")

</div>

![Calculate%20age%20from%20birthdate](https://us1.discourse-cdn.com/bubble/original/3X/b/0/b0af58d3134c6b88e2e8f4ed67e9ae634e86890b.png)

---

<div class="post-metadata">

**Author:** ![sanchezy.business](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sanchezy.business](https://forum.bubble.io/u/sanchezy.business)\
**Post date:** [January 7, 2020, 8:00pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/14 "2020-01-07T20:00:27Z")

</div>

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](https://exceljet.net/excel-functions/excel-datedif-function).  
I tried several options where I’m subtracting however I don’t believe it works for all corner cases.

 ![Capture](https://us1.discourse-cdn.com/bubble/original/3X/5/9/59bd6f4fb67e2d4933b5f6d03f2fa64d9cb58723.png)  
I’m capturing the birth date from an input and transforming it into years, months, days and hours.  
 ![Capture1](https://us1.discourse-cdn.com/bubble/original/3X/4/e/4e7829d40c45aeae10956f331f064a9f505b109d.png)  
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?

---

<div class="post-metadata">

**Author:** ![sanchezy.business](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sanchezy.business](https://forum.bubble.io/u/sanchezy.business)\
**Post date:** [February 7, 2020, 7:49pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/15 "2020-02-07T19:49:40Z")

</div>

Any luck? 🙄

---

<div class="post-metadata">

**Author:** ![bebungbia](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bebungbia](https://forum.bubble.io/u/bebungbia)\
**Post date:** [July 20, 2020, 5:23pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/16 "2020-07-20T17:23:38Z")

</div>

It’s here buddy 😉

![image](https://us1.discourse-cdn.com/bubble/original/3X/8/f/8fc479bd4f883b17695b62c34810005d5178e05b.png)

---

<div class="post-metadata">

**Author:** ![dellean.santos](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/dellean.santos/32/66857_2.png) [@dellean.santos](https://forum.bubble.io/u/dellean.santos)\
**Post date:** [July 20, 2020, 8:39pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/17 "2020-07-20T20:39:31Z")

</div>

Fucking Genius!

---

<div class="post-metadata">

**Author:** ![austin3](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/austin3/32/69385_2.png) [@austin3](https://forum.bubble.io/u/austin3)\
**Post date:** [August 19, 2020, 5:16pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/18 "2020-08-19T17:16:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![austin3](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/austin3/32/69385_2.png) [@austin3](https://forum.bubble.io/u/austin3)\
**Post date:** [October 13, 2020, 3:19pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/19 "2020-10-13T15:19:08Z")

</div>

> [@PWC](#):
>
> ```auto
> 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"));
> 
> ```

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!

---

<div class="post-metadata">

**Author:** ![NigelG](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@NigelG](https://forum.bubble.io/u/NigelG)\
**Post date:** [January 21, 2021, 9:11am UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/20 "2021-01-21T09:11:19Z")

</div>

> [@austin3](#):
>
> @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

Remarkably it is still working 🙂

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

---

<div class="post-metadata">

**Author:** ![NigelG](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@NigelG](https://forum.bubble.io/u/NigelG)\
**Post date:** [January 26, 2021, 12:09pm UTC](https://forum.bubble.io/t/how-calculate-user-age/9038/21 "2021-01-26T12:09:12Z")

</div>

> [@austin3](#):
>
> Hope this helps!

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

[Next page](https://forum.bubble.io/t/how-calculate-user-age/9038.md?page=2)
