locked
Adding Days to a date not working RRS feed

  • Question

  • I am trying to add 120 days to a date when the user fills in a date field on a form. I have the code below which seems to work ok for dates in the middle of the year, but if the supplied date is near the end of the year, causing the year to roll over, it seems to go completely wrong! Any ideas?

    function SetRenewalDate(){
    
    
    var CED = Xrm.Page.getAttribute("tbw_elecced").getValue();
    
    var NewDate = new Date();
    
    NewDate.setDate(CED.getDate()+120);
    
    Xrm.Page.getAttribute("tbw_stdate").setValue(NewDate);
    }

    Monday, July 8, 2013 9:49 AM

All replies

  • You just need to add the days to the first date object, is not necessary to create a new date object. 

    function SetRenewalDate() {
    var CED = Xrm.Page.getAttribute("tbw_elecced").getValue();
    
    CED.setDate(CED.getDate()+120);
    
    Xrm.Page.getAttribute("tbw_stdate").setValue(CED);
    }
    example: if the first date is 1 October 2013, the result date will be 29 January 2014



    My blog: www.crmanswers.net


    Monday, July 8, 2013 10:27 AM
  • Thank you very much! But, out of interest, why did it not working when creating a new date object? Surely both methods should work?

    Thanks again,

    Chris

    Monday, July 8, 2013 10:34 AM
  • Your old code will set only the days from the first date to today's date plus 120.

    Example:

    tbw_elecced date is 5 September 2013 and today is 8 July 2013.

    Your code will do 5 + 120 and set as days to the NewDate object and the result date (wrong) will be 2 November 2013 instead of the right 3 January 2014.


    My blog: www.crmanswers.net

    Monday, July 8, 2013 11:05 AM
  • Please have a look at this Link

    It explains the reason.

    Regards



    • Edited by jattscorpion Monday, July 8, 2013 12:49 PM correction
    Monday, July 8, 2013 12:13 PM