4 Replies Latest reply on Jul 25, 2012 7:42 AM by Neil-Kentucky

    Calculate Months from Field Date

    Neil-Kentucky Level 1

      Using: Acrobat X Pro in windows. 

       

      I have three fields:

      1) Today's Date:___________ [DateToday] -- Automatically filled-in with today's date using script.

      2) Date Last Payment Made:__________ [LastPaymentMade1] -- Filled-in by user in mm/dd/yyyy format.

      3) Months Since Last Payment:______________ [MoBehind] -- Need to calculate from field 1 - 2).

       

      For #3, how do I custom calculate how many MONTHS it has been (#3) since the date the last payment was made (#2)?

       

      There's a great discussion here on this topic but I cannot figure out how to modify the script for my purposes: http://forums.adobe.com/message/1907993#1907993

       

      This appears to be the closest thing to what I want:

       

      function Floor(fValue) { 
      return Math.floor(fValue); 
      } 
      
      
      function Date2Num(sDate, sFormat) { 
      var fSecond =1000; 
      var fMinute = 60 * fSecond; 
      var fHour = 60 * fMinute; 
      var fDay = 24 * fHour; 
      var oDate = util.scand(sFormat, sDate); 
      var fDate = oDate.valueOf(); 
      var fDate = fDate / fDay; 
      var fDate = Floor(fDate); 
      return fDate; 
      } 
      var Start = this.getField("DateToday").value; 
      var sFormat = "mm/dd/yyyy"; 
      var End = this.getField("LastPaymentMade1").value; 
      var sFormat = "mm/dd/yyyy"; 
      var eFormat = sFormat; 
      event.value = Date2Num(End, eFormat) - Date2Num(Start, sFormat); 
      
      

       

      Here's another one that looks SIMILAR to what i want:

       

       

      (function () {
      
          // Get date from field
          var v = getField("DateToday").value;
      
          if (v) {
      
              // Convert string to date
              var d = util.scand('mm/dd/yyyy', v);
      
              // Add number of days in NUMDAYS field
              d.setDate(d.getDate() - +getField("NumDays").value);
      
              // Set value of this field to the new date
              event.value = util.printd("mm/dd/yyyy", d);
      
      
          } else {
      
              // Blank field if no date entered
              event.value = "";
          }
      })();
      
      
        • 1. Re: Calculate Months from Field Date
          gkaiseril MVP & Adobe Community Professional

          Since the number of days in a month are not constant across a year I do not think you can use the milliseconds in a day.

           

          I you use the 'getMonth()' and 'getFullYear()' methods for the calculation.

           

          // get the date values

          var Start = this.getField("DateToday").value;

          var sFormat = "mm/dd/yyyy";

          var End = this.getField("LastPaymentMade1").value;

          var sFormat = "mm/dd/yyyy";

          var eFormat = sFormat;

          // convert to date object

          var oStart = util.scand(sForamt, Start);

          var oEnd = util.scand(eFormat, End);

          // get start month

          var StartMonth = oStrat.getMonth();

          // add start full year as months

          StartMonth = StartMonth + (oStart.getFullYear() * 12);

          // get end month

          var EndMonth = oEnd.getMonth();

          // add end full year as months

          EndMonth = EndMonth + (oEnd.getFullYear() * 12);

          // compute difference in months

          var diffMonths = EndMonth - StratMonth;

          • 2. Re: Calculate Months from Field Date
            Neil-Kentucky Level 1

            That looks good.  How do I associate the getFullYear() and other field to the field dates on my form?

            • 3. Re: Calculate Months from Field Date
              Neil-Kentucky Level 1

              I found a typo in the code, just a minor one, but it still isn't working when I place the code in the custom calculation script in my form.  I've ensured that therer is data in the corresponding fields and yet the field is blank.

              • 4. Re: Calculate Months from Field Date
                Neil-Kentucky Level 1

                PS, in the java console, the output is:

                 

                "

                ReferenceError: EndMonth is not defined

                1:Console:Exec

                undefined

                "