1 Reply Latest reply on Dec 5, 2014 2:48 PM by gkaiseril

    Count Elapsed Days

    adobe only

      Hi Community,

       

      I have posted this question in Acrobat.Answers.com

      Count Elapsed Days (JavaScript)

       

      The script provided there is almost works. But If SaleesDate is Jan 1, then this is showing wrong output. ie;

      Jan 1, 2014 showing 3366, but should be 41. Only issue if Date is Jan 1.

       

      Also i have two more request on this same.

      1. If SalesDate is empty, then show as empty.

      2. Always use 000 format in elapsed days. so need to show like 001, 010, 100. So with Lastdigit is like 4001, 4010, 4100

       

      Thanks a lot for your valuable time.

       

      Thank you

      AdobeFan

        • 1. Re: Count Elapsed Days
          gkaiseril MVP & Adobe Community Professional

          There are a number of trip points here.

           

          Try the following script for the year and  elapsed number of days field:

           

          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;

           

           

          event.value = ""; // clear the result field;
          var cFieldName = "SalesDate"; // field name;
          var oSalesField = GetField(cFieldName); // get field object;
          if(oSalesField != null && oSalesField.valueAsString != "") {
          oSalesDate = Scand("dd-mmmm-yyyy", oSalesField.valueAsString); // convert date string to date object;
          // console.println(oSalesDate + " sales date");
          var oStartDate = Scand("yyyy-mm-dd", oSalesDate.getFullYear() + "-01-01"); // date object for 1-jan for year of sale;
          // console.println(oStartDate  + " Start date");
          var nDiff = oSalesDate.getTime()  - oStartDate.getTime(); //elapsed milliseconds;
          nDiff = Math.ceil(nDiff / (1000 * 60 * 60 * 24)) + 1; // days inlcuding last day;
          // console.println(nDiff);
          // set field value inlcude last digit of year and elapsed days;
          event.value = String(oSalesDate.getFullYear()).substr(3,1) + util.printf("%,103.0f", nDiff);
          }

           

          One has to count the start day as a full day and then there is the 3 leading zero formatting.