Calculate Age only if Date of Birth field is captured

Comments

16 comments

  • Hamid

    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...

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    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

    0
    Comment actions Permalink
  • Heather Lockhart

    Many thanks Hamid and Rainer. I got it to work

    0
    Comment actions Permalink
  • 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 :)

    0
    Comment actions Permalink
  • John Beasley

    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;

    0
    Comment actions Permalink
  • Rainer Grabowski

    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).  

    0
    Comment actions Permalink
  • Jakob Rosenfeldt

    is there any way it could be updated automatically? 

    0
    Comment actions Permalink
  • Rainer Grabowski

    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

     

    0
    Comment actions Permalink
  • Rachel Gretton

    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.

    0
    Comment actions Permalink
  • Jakob Rosenfeldt

    @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

    0
    Comment actions Permalink
  • John Beasley

    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-

    0
    Comment actions Permalink
  • Isabelle Schmitz

    Hi, 

     

    when i use : 

     

    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


    I receive message : the result is not a defined number ? 

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Isabelle,
    can you please share a screenshot of your calculation (incl. the error message)?
    Rainer

    0
    Comment actions Permalink
  • Isabelle Schmitz

    Rainer Grabowski

    here

     

     

     

    and here in new app

     

    0
    Comment actions Permalink
  • Henning Jeppesen

    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 : null

    Hope some help to figure out this issue

     

    0
    Comment actions Permalink
  • Podio Admin

    Rainer Grabowski

     

    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

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk