locked
JavaScript to get 65th birthday RRS feed

  • Question

  • I need help creating a field with the 65th birthday.

    I have a field birthdate and I would like to use Javascript to return the date of the 65th birthday so that I can report on it and send workflows.

    Please HELP!


    Kristen Hughes

    Friday, September 19, 2014 3:40 PM

Answers

  • Hi,

    You could use something like the code below:

    function Birthday() {
    var Birth = Xrm.Page.getAttribute("birthday").getValue();
    var Birth65 = new Date(Birth.getFullYear() + 65,Birth.getMonth(),Birth.getDay());
    Xrm.Page.getAttribute("new_65birth").setValue(Birth65);
    Xrm.Page.data.entity.save();
    }


    Persian Dynamics CRM Community | انجمن متخصصان فارسی زبان

    • Marked as answer by Kristen Hughes Friday, September 19, 2014 5:14 PM
    Friday, September 19, 2014 4:28 PM
    Moderator
  • Payman's code contains a small mistake, the function to get the day is getDate, not getDay

    so the correct line is:

    var Birth65 = new Date(Birth.getFullYear() + 65,Birth.getMonth(),Birth.getDate());

    in addition if the birthday is on 29 February with the standard javascript function the 65 birthday will be on 1 March, some parts of the world (for example Taiwan) consider the legal birthday for the leap year born on 28 February.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Kristen Hughes Friday, September 19, 2014 5:14 PM
    Friday, September 19, 2014 4:57 PM

All replies

  • Hi,

    You could use something like the code below:

    function Birthday() {
    var Birth = Xrm.Page.getAttribute("birthday").getValue();
    var Birth65 = new Date(Birth.getFullYear() + 65,Birth.getMonth(),Birth.getDay());
    Xrm.Page.getAttribute("new_65birth").setValue(Birth65);
    Xrm.Page.data.entity.save();
    }


    Persian Dynamics CRM Community | انجمن متخصصان فارسی زبان

    • Marked as answer by Kristen Hughes Friday, September 19, 2014 5:14 PM
    Friday, September 19, 2014 4:28 PM
    Moderator
  • That is extremely helpful in the fact that it actually calculates the year properly, however my original birthdate Day is the 25th and the new field shows the 4th.  Original birthdate is 6/25/1959 and 65th birthdate field is 6/4/2024.  Any ideas why?

    Kristen Hughes

    Friday, September 19, 2014 4:53 PM
  • Payman's code contains a small mistake, the function to get the day is getDate, not getDay

    so the correct line is:

    var Birth65 = new Date(Birth.getFullYear() + 65,Birth.getMonth(),Birth.getDate());

    in addition if the birthday is on 29 February with the standard javascript function the 65 birthday will be on 1 March, some parts of the world (for example Taiwan) consider the legal birthday for the leap year born on 28 February.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Kristen Hughes Friday, September 19, 2014 5:14 PM
    Friday, September 19, 2014 4:57 PM
  • Sorry. That was my fault.

    Use getDate() instead of getDay().


    Persian Dynamics CRM Community | انجمن متخصصان فارسی زبان

    Friday, September 19, 2014 4:57 PM
    Moderator
  • That is wonderful!  Works like a charm!  Thank you VERY much!

    Kristen Hughes

    Friday, September 19, 2014 5:15 PM