1 Reply Latest reply on Dec 22, 2014 2:51 PM by gkaiseril

    Calculate days elapsed between two date fields

    LeonaB17

      I need a script to determine the number of days elapsed between two date fields. I have searched the FAQ's for this topic but have not found an answer that works for me yet.

       

      DATE1 is begin date field with MM/DD/YY format, DATE2 is end date with MM/DD/YY format, and DAYSEL is the days elapsed between DATE1 and DATE2.

       

      I am not proficient with javascript so I would greatly appreciate if someone could tell me what I need to put on the calculate tab of DAYSEL to run this calculation.

       

      Any help much appreciated!

        • 1. Re: Calculate days elapsed between two date fields
          gkaiseril MVP & Adobe Community Professional


          I would use:

           

          // common document functions;


          function GetField(cName) {
          // get field object with error checking;
          var oField = this.getField(cName);
          if(oField == null) app.alert("Error accessing field named " + cName + "\nPlease check that this is the correct field name.", 1, 0);
          return oField;
          } // end GetField function;

          function Scand(cFormat, cDateString) {
          // using format convert date string to date object with error checking;
          var oDate = util.scand(cFormat, cDateString);
          if(oDate == null) app.alert("Error converting date " + cDateString + " with format of " + cFormat + "\nPlease check date string and date formatting.", 1, 0);
          return oDate;
          } // end Scand function;

          // end common document functions;

           

          // custom calculation script;

          event.value = 0;

          var cDateFormat = "mm/dd/yy"; // format for date fields;

          var cStartName = "DATE1"; // field name;

          var nDiff = 0;

          var oStartField = GetField(cStartName); // get field object;

          if(oStartField != null && oStartField.valueAsString != "") {

          oStartDate = Scand(cDateFormat, oStartField.valueAsString); // convert date string to date object;

          }

          var cEndName = "DATE2"; // field name;

          var oEndField = GetField(cEndName); // get field object;

          if(oEndField != null && oEndField.valueAsString != "") {

          oEndDate = Scand(cDateFormat, oEndField.valueAsString); // convert date string to date object;

          }

          if(oStartDate != null && oEndDate != null) {

          nDiff = oEndDate.getTime()  - oStartDate.getTime(); //elapsed milliseconds;

          event.value = Math.floor(nDiff / (1000 * 60 * 60 * 24)); // elapsed days;

          }

          // end custom calculation script;

           

          If needed you can put all the code in the custom calculation. The code includes error checking for wrong fields and bad date/time fields.