2 Replies Latest reply on Jan 20, 2015 12:34 PM by ITProPhoto

    Javascript Subtracting Dates

    ITProPhoto

      First, any help that is accurate is greatly appreciated.

      I am creating a form for our HR office which needs to subtract dates and turn the result into a yes or a no.  I have not done dates in JavaScript before so i am having troubles.

      I have 3 fields. TerminationDate, EffectiveDate which are in mm/dd/yyyy format, and DaysResult field that is Text format. What i am trying to do is have a script in DaysResult field runs something like TerminationDate-EffectiveDate=Days. Then have If Days =or> 90 then DaysResult=YES. If Days < 90 then DaysResult=NO

      Make sense?

        • 1. Re: Javascript Subtracting Dates
          gkaiseril MVP & Adobe Community Professional

          You have to use the Acrobat JavaScript "util.scand" method to create the date object for each date and then use the "getTime()" method to get the number of milliseconds since the Epoch Date, midnight January 1, 1970. You can then do the math to get the difference of the two values and convert the result to days. Then it is a simple "if" statement to set the text field.

           

           

          // doucment level scripts;
          function GetField(cName) {
          // get field object for field named cName;
          var oField = this.getField(cName); // get field object;
          if(oField == null) {
            // null value is error condition;
            app.alert("Error getting field named \"" + cName + "\"!", 0, 0);
          } // end error trapping and reporting;
          return oField;
          } // end GetField function;

          function Scand(cFormat, cString) {
          // util.scand with error chedking;
          var oDate = util.scand(cFormat, cString);
          if(oDate == null) app.alertr("Error converting date value " + cString + " with format image of " + cFormat, 0, 1);
          return oDate
          } // end Scand function;

          function Date2Days(cFormat, cString) {
          var oDate = Scand(cFormat, cString);
          if(oDate == null) app.alertr("Error converting date value " + cString + " with format image of " + cFormat, 0, 1);
          var nDate = oDate.getTime();
          return Math.floor(nDate / (1000 * 60 * 60 * 24));
          }
          // end document level scripts;

           

           

          // custom JavaScript calculation to compute difference in days between 2 dates;

          // field names;

          var cStartField = "EffectiveDate";

          var cEndField = "TerminationDate";

          // date format image;

          var cDateFormat = "mm/dd/yyyy";

           

          function FunctionExist(cName) {
          // see if a function exist;
          var bExist = eval("typeof " + cName + " == \"function\"");
          if(!bExist) app.alert("Missing needed function\" " + cName + "\"!\nPlease install the necessary function.", 1, 0);
          return bExist;
          } // end FunctionExist function;


          // test for user written functions that are needed for this script;
          FunctionExist("GetField");
          FunctionExist("Scand");
          FunctionExist("Date2Days");

           

          event.value = "";
          // get field objects;
          var oStart = GetField(cStartField);
          var oEnd = GetField(cEndField);
          if(oStart.valueAsString != "" && oEnd.valueAsString != "") {
          event.value = "No";
          var nStart = Date2Days(cDateFormat, oStart.valueAsString);
          var nEnd = Date2Days(cDateFormat, oEnd.valueAsString);
          var nDiff = nEnd - nStart;
          // test for difference greater than 90;
          if(nDiff > 90) {
            event.value = "Yes";
          }
          /*
          // debugging informattion;
          console.show();
          console.clear();
          conole.println("Start: " + oStart.valueAsString);
          console.println("End: " + oEnd.valueAsString);
          console.println("Start days: " + nStart);
          console.println("End days: " + nEnd);
          console.println(nEnd - nStart);
          console.println("Days: " + nDiff);
          // end debuggig information;
          */
          }
          // end custom calculaion script;

          • 2. Re: Javascript Subtracting Dates
            ITProPhoto Level 1

            You my friend, are amazing!