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?
-
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()
orDate.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
-
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 : nullBut be aware that the age wouldn't be updated (s. my example right below the code in my last post).
-
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 : nullThen 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'
Thanks in advance.
-
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 : nullAnd using another calculation field for the check date using this:
https://help.podio.com/hc/en-us/community/posts/207173447-Date-field-current-date-
-
here
and here in new app
-
I know this is an old post....
Have som problms..
Field "Udregning 2" works but I can't use the only one post from my CD
var date = @Fødselsdag;
var dateCalc = moment(@Max of CD);
var currentDate = moment(@All of CD);
var calc = currentDate.diff(dateCalc, 'y');
date != null ? calc : nullHope some help to figure out this issue
-
Using your calculation, the result always comes back as null and I am unsure why. Can you help?
This one does work but obviously doesn't take into account leap years
Please sign in to leave a comment.
Comments
16 comments