Calculate current year birthday based off birthday field



  • Nathan Velasquez
    I think I got it. Had to extract year, month and date separately. Then get the current date. Use If statement to check if current month is greater than birth month, if so set birthdate to next year, else set it to this year.
    // Get Date of Birth Entry
    var dOb = @DOB;
    // Extract Birth Month
    var birthMonth = dOb.getMonth();
    // Extract Birth Date
    var birthDate = dOb.getDate();
    // Extract Birth Year
    var birthYear = dOb.getFullYear();
    // Get Today's Date
    var today = new Date();
    // Get Today's Month
    var curMonth = today.getMonth();
    // Get Today's Year
    var curYear = today.getFullYear();
    // Get This Year's Birthday
    var curYearBirthday = new Date(curYear, birthMonth, birthDate);
    // Get Next Year's Birthday
    var nextYearBirthday = new Date(curYear +1, birthMonth, birthDate);
    // If the current month is bigger than than the birth month set this date to the birthday of next year
    if (curMonth > birthMonth)
    // Else, set the birthday to this year's birthday
    Comment actions Permalink
  • Rainer Grabowski

    Hi Nathan,

    sorry to disappoint you: But var today = new Date(); doesn't work. You'll  get the date of the day you save the template (or a new item) - next year you'll have the same 2018 date. 

    More about this issue:


    Comment actions Permalink
  • Felix Lepoutre

    var date = @Geboortedatum

    diffYears = moment().diff(moment(date),'years');

    if(moment().format("M") < moment(date).add(diffYears, 'years').format("M")){
    diffYears += 1;
    moment(date).add(diffYears, 'years').toDate()

    Comment actions Permalink
  • Rainer Grabowski

    That doesn't work. See my comment above. In the calculation you need a variable which changes every day. 

    Comment actions Permalink
  • Felix Lepoutre - Private

    Oh wow, thank you!


    Comment actions Permalink
  • Manni Sidhu

    Hi guys,

    Has anyone managed to get a birthday field to work with a calculation?

    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk