locked
help with a date function RRS feed

  • Question

  • Hi,

    We have had a small piece of java script written which does the following. You choose a start date, and how many months the contract is for, and then it should increase the start date by those months. So first 1st jan start date on 6 months contract should have a calculated end date of 30th june.

    Below is the code which is currently adds 1 extra month. Any ideas?

    //PC 30/7/10 Onchange triggers the calculation of number of months times the monthly value
    var init = crmForm.all.new_initialpayment.DataValue;
    var months = crmForm.all.new_numberofmonths.DataValue;
    var monthpay = crmForm.all.new_monthlyvalue.DataValue;
    crmForm.all.new_totalvalue.DataValue = (months * monthpay) + init;

    //PC 6/8/10 Onchange also triggers the calculation of project end date
    var projectstart = crmForm.all.new_contractmethodstarted.DataValue;
    var months = crmForm.all.new_numberofmonths.DataValue;
    var projectend = crmForm.all.new_projectenddate.DataValue;

    oDate = projectstart;

    if (projectstart == null) {
        oDate = new Date();
    }


    var newMonth = oDate.getMonth() + months;
    var newYear = oDate.getYear();

    if(newMonth>11)
    {
         newYear++;
         newMonth = newMonth%11;
    }

    projectend = oDate.setMonth(newMonth);
    projectend = oDate.setYear(newYear);

    crmForm.all.new_projectenddate.DataValue = oDate;
     
    //PC 6/8/10 - Onchange also triggers the calculation of payment end date
    var firstpay = crmForm.all.new_firstpayment.DataValue;
    var months = crmForm.all.new_numberofmonths.DataValue;
    var lastpay = crmForm.all.new_lastpayment.DataValue;

    oDate = firstpay;

    if (firstpay == null) {
        oDate = new Date();
    }


    var newMonth = oDate.getMonth() + months;
    var newYear = oDate.getYear();

    if(newMonth>11)
    {
         newYear++;
         newMonth = newMonth%11;
    }

    lastpay = oDate.setMonth(newMonth);
    lastpay = oDate.setYear(newYear);

     

    crmForm.all.new_lastpayment.DataValue = oDate;

     

    Wednesday, August 11, 2010 11:06 AM

Answers

  • SetMonth() - Sets the month, given a number between 0-11

    You must be starting from 1-12. Please make it 0 to 11

    See the below link for more information:

    http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

    Hope this helps.


    Thanks, Ranjitsingh R | http://mscrm-developer.blogspot.com/ | MS CRM Consultant
    Wednesday, August 11, 2010 11:12 AM
  • Hi

    Write a simple web service in C# to calculate this sort of functionality and use GET Web Service from the crmForm. this is very easy and very extensible.

    js function to add months is

    Date.prototype.addMonths = function (n) {
          var day = this.getDate();
         
          this.setMonth(this.getMonth() + n);
          this.setDate(this.getDate() - 1);

          return this;
      }
      var d = new Date("04/01/2010"); // Replace this with your crmDate Attribute, usage is much like extensible methods in .net
      alert(d.addMonths(6));

    Hope this helps for more use http://bytes.com/topic/javascript/answers/93512-add-six-months-start-date

    Amar

    Wednesday, August 11, 2010 1:00 PM

All replies

  • SetMonth() - Sets the month, given a number between 0-11

    You must be starting from 1-12. Please make it 0 to 11

    See the below link for more information:

    http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

    Hope this helps.


    Thanks, Ranjitsingh R | http://mscrm-developer.blogspot.com/ | MS CRM Consultant
    Wednesday, August 11, 2010 11:12 AM
  • Hi

    Write a simple web service in C# to calculate this sort of functionality and use GET Web Service from the crmForm. this is very easy and very extensible.

    js function to add months is

    Date.prototype.addMonths = function (n) {
          var day = this.getDate();
         
          this.setMonth(this.getMonth() + n);
          this.setDate(this.getDate() - 1);

          return this;
      }
      var d = new Date("04/01/2010"); // Replace this with your crmDate Attribute, usage is much like extensible methods in .net
      alert(d.addMonths(6));

    Hope this helps for more use http://bytes.com/topic/javascript/answers/93512-add-six-months-start-date

    Amar

    Wednesday, August 11, 2010 1:00 PM