locked
Calculate Age from Date of Birth field RRS feed

  • Question

  • I have a date of birth field called DOB and I have an Age field called Age. Date of birth is input as dd/mm/yyyy in DOB.

    I want to get the Age from the DOB and display in the Age field.

    I would like to do this as a javascript function.

    Thanks.

    Monday, May 21, 2012 2:12 PM

Answers

  • The below function works for me every time
    I hope this will works for you to
    function CalcAge()
    {
        var now = new Date(); //Todays Date   
        var birthday = Xrm.Page.getAttribute("arcrp_dob").getValue(); //Get the Date of Birth value  
         
        var diff = now.getMonth() - birthday.getMonth();  //Check to see if Birthday has already passed
    if (diff > -1) //If Birthday has already occurred   
        {
            var bd1 = now.getFullYear() - birthday.getFullYear();
            //set the age attribute 
            Xrm.Page.getAttribute("arcrp_age").setValue(bd1.toString());  
        }
        else //If Birthday has not already occurred  
        {
            var bd2 = now.getFullYear() - birthday.getFullYear() - 1;
            Xrm.Page.getAttribute("arcrp_age").setValue(bd2.toString()); 
        }
    }

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!


    • Edited by MubasherSharif Monday, May 21, 2012 2:26 PM
    • Proposed as answer by MubasherSharif Monday, May 21, 2012 2:31 PM
    • Marked as answer by JMcCon Tuesday, May 22, 2012 8:51 AM
    Monday, May 21, 2012 2:25 PM
  • Can you try something like this:

    function findAge() {
    
        var today = new Date();
        var birthdate = new Date(Xrm.Page.getAttribute("birthdate").getValue());
        var age = today.getFullYear() - birthdate.getFullYear();
        var m = today.getMonth() - birthdate.getMonth();
        if (m < 0 || (m === 0 && today.getDate() < birthdate.getDate())) {
            age--;
        }
    
        Xrm.Page.getAttribute("Age").setValue(age.toString());
    
    }
    

    You'll have to work out when you call this to make sure your Age field is always accurate.


    Jason Lattimer

    Monday, May 21, 2012 2:28 PM
    Moderator

All replies

  • The below function works for me every time
    I hope this will works for you to
    function CalcAge()
    {
        var now = new Date(); //Todays Date   
        var birthday = Xrm.Page.getAttribute("arcrp_dob").getValue(); //Get the Date of Birth value  
         
        var diff = now.getMonth() - birthday.getMonth();  //Check to see if Birthday has already passed
    if (diff > -1) //If Birthday has already occurred   
        {
            var bd1 = now.getFullYear() - birthday.getFullYear();
            //set the age attribute 
            Xrm.Page.getAttribute("arcrp_age").setValue(bd1.toString());  
        }
        else //If Birthday has not already occurred  
        {
            var bd2 = now.getFullYear() - birthday.getFullYear() - 1;
            Xrm.Page.getAttribute("arcrp_age").setValue(bd2.toString()); 
        }
    }

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!


    • Edited by MubasherSharif Monday, May 21, 2012 2:26 PM
    • Proposed as answer by MubasherSharif Monday, May 21, 2012 2:31 PM
    • Marked as answer by JMcCon Tuesday, May 22, 2012 8:51 AM
    Monday, May 21, 2012 2:25 PM
  • Can you try something like this:

    function findAge() {
    
        var today = new Date();
        var birthdate = new Date(Xrm.Page.getAttribute("birthdate").getValue());
        var age = today.getFullYear() - birthdate.getFullYear();
        var m = today.getMonth() - birthdate.getMonth();
        if (m < 0 || (m === 0 && today.getDate() < birthdate.getDate())) {
            age--;
        }
    
        Xrm.Page.getAttribute("Age").setValue(age.toString());
    
    }
    

    You'll have to work out when you call this to make sure your Age field is always accurate.


    Jason Lattimer

    Monday, May 21, 2012 2:28 PM
    Moderator
  • Thanks for the help guys.
    Tuesday, May 22, 2012 8:51 AM