### Post

8 followers Follow

# Calculate Age only if Date of Birth field is captured

Hi,
I have created a calculation field to calculate the age of a person based on their date of birth:
(Date.now() - @Date of Birth)/31536000000

If there is no date of birth entered the calculation field is defaulting to 45. Is there a way I can change my calculation to only calculate the age if the date of birth field has been captured, and to leave the calculation field blank if there is no date of birth?

Heather Lockhart

Hi Heather,
You can use if else statement to performe this :

``````var birthDay = Math.floor((Date.now() - @Date of Birth)/31536000000);
@Date of Birth != null ?  birthDay : null

``````

But the problem with this calculation is the update not working every year if there is no changes in any field in the app !
you can use a related app with one date_Field witch you update manually and insert it in the calculation field...

Hi Heather,

your formula can produce the wrong result cause the divider "31536000000" (365 x 24 x 60 x 60 x 1000)
doesn't consider leap days. Try birthday June 7, 2000 and you'll get today (June 4, 2015) the result "15 years" (cause there have been 3 leap years between Date of Birth and today). This will show the right result including an IF-Condition "if there is no date of birth leave the calculation field blank":

``````var date  = @Date of Birth;
var dateCalc = moment(date);
var currentDate = moment();
var calc = b.diff(a, 'y');

date != null ? calc : null
``````

BUT: The date for "today" which you can get by `moment()` or `Date.now()` is static. Which means: If you enter the date of birth "June 5, 2000" today (= June 4, 2015) the correct result is 14. But tomorrow on June 5 it still will be 14 instead of being updated to 15 - the value of "currentDate" will stay the same value as it is in the moment you enter the date of birth.

A calculation will only run if one value in the calculation is changed. You would need a field "Check Date" which has to be updated daily (than "var currentDate = moment(@Check Date)". You could update this "Check Date" automatically with the Podio extension Globiflow. Or you create an extra app e.g. called "Today" , with only one item "Date today" with only one date field, link that item via a relationship field in your birthday App (you have to set the relation in every item) and update each day the date field in "Date today" (than: var currentDate = moment(@all of dateTodayDateField.join())

Rainer

Many thanks Hamid and Rainer. I got it to work

for some reason it didn't work for me, I've modifiedthecode to this and it worked just fine for me:

var d1 = moment(@ Birth Date);
var d2 = moment();
var result = d2.diff(d1, 'years');
result;

hope that helps and thnx for the inspiration :)

Ibrahim S - Brazilian Experience 0 votes

Hey @Rainer,

I was not able to get calculation to work.

Ibrahim S solution worked, but I had to add a one to it in order for it to show the correct number of years:

var d1 = moment(@ Birth Date);
var d2 = moment();
var result = d2.diff(d1, 'years');
result + 1;

uuups, sorry, my calculation can't work cause I've forgotten to change the variables a and b  in the line var calc (I've copied it from an old example). Correct version is:

`var date = @Date of Birth; var dateCalc = moment(date); var currentDate = moment(); var calc = currentDate.diff(dateCalc, 'y');date != null ? calc : null`

But be aware that the age wouldn't be updated (s. my example right below the code in my last post).

is there any way it could be updated automatically?

Only with cumbersome workarounds. You could create a new app with 1 item only, which has only 1 field, a date field. Then add a relationship field in the app where you hold the birthdate and relate each item to this one item in the new app. And keep attention that each new item will be related. In your caculation field you can do:

`var date = @Date of Birth; var dateCalc = moment(@max of datefield in your new app); var currentDate = moment(); var calc = currentDate.diff(dateCalc, 'y');date != null ? calc : null`

Then update the item in the new app manually every day.

Or you can use the extension Globiflow. Then a date field in the app with the birth date and let Globiflow it update daily.

Rainer

I hope someone can help me with the following (I've tried all of the options below but can't get any of them to work)

We are referencing the date of birth field from a calc field that takes three category fields (day, month and year) to create a date e.g. 24/02/2016. We've done it this way rather than use a date field so that we don't have to scroll back through years and years to get a birth year.

Is this what is causing me the problem because I keep getting an error saying 'not a vaild number'

@Rachel

You don't have to scroll.

You can always type out the date in the date field. Keep the format like this DD/MM/YYYY

Hi Everyone,

I was able to find a solution using Rainer's solution:

var date = @Date of Birth;
var dateCalc = moment(date);
var currentDate = moment(@[Birth Date Check]);
var calc = currentDate.diff(dateCalc, 'y');
date != null ? calc : null

And using another calculation field for the check date using this:

https://help.podio.com/hc/en-us/community/posts/207173447-Date-field-current-date-