2 Replies Latest reply on Jul 24, 2013 11:35 AM by ClemmBallina

    If/Else Javascript Request - Date Calculations

    ClemmBallina

      Hi Everyone,

       

      I am working on a Travel Authorization Request form. There is a section that asks for a departure date, return date, and total days.

       

      In the total days field, I have placed a script that calculates the number of days based on the departure and return date, plus 1 day. This is so that if the dates are 06/01/13 - 06/03/13, the total days is 3, not 2.

       

      The script is below:

       

      var sDate1=this.getField("Return Date").value;
      var sDate2=this.getField("Departure Date").value;
      var oDate1 = util.scand("mm/dd/yyyy", sDate1);
      var oDate2 = util.scand("mm/dd/yyyy", sDate2);

      var nDay = 1000 * 60 * 60 * 24;

      var eDays=Math.abs(oDate1-oDate2)/nDay + 1;

      event.value=Math.round(eDays);

       

      The problem is, when the departure and return date fields are blank, there is still a 1 in the total days field.

      Picture1.png

      I need to add to this script, so that when the departure and return date fields are blank, nothing, or atleast a zero, appears in the total days field.

       

      Please note, I am completely new to javascript and the one I used above, I found through internet searches.

       

      Thanks :-)

        • 1. Re: If/Else Javascript Request - Date Calculations
          gkaiseril MVP & Adobe Community Professional

          You can check to make sure that both fields are not blank before conditionally computing the number of days.

           

          Conditional execution and Acrobat JavaScript by Thom Parker

           

          var sDate1=this.getField("Return Date").valueAsString;
          var sDate2=this.getField("Departure Date").valueAsString;

           

          // conditional execution only when both fields have data

          if(sDate1 != "" && sDate2 != "") {
          var oDate1 = util.scand("mm/dd/yyyy", sDate1);
          var oDate2 = util.scand("mm/dd/yyyy", sDate2);

          var nDay = 1000 * 60 * 60 * 24;

          var eDays = (Math.abs(oDate1- oDate2) / nDay) + 1;

          event.value = Math.round(eDays);

          } else {

          // if both fields empty clear the field;

          event.value = "";

          }

           

          I would have converted the date object to a value and turncated the value to the day unit.

           

          // get fields as string values;
          var sDate1=this.getField("Return Date").valueAsString;
          var sDate2=this.getField("Departure Date").valueAsString;
          // value of field if calculation not performed;
          event.value = "";
          // conditional execution only when both fields have data
          if(sDate1 != "" && sDate2 != "") {
          // convert date strings to date object
          var oDate1 = util.scand("mm/dd/yyyy", sDate1);
          var oDate2 = util.scand("mm/dd/yyyy", sDate2);
          var nDay = 1000 * 60 * 60 * 24;
          // convert date objects to number of days since epoch date and turncate to days
          var nDate1 = Math.floor(Number(oDate1) / nDay)
          var nDate2 = Math.floor(Number(oDate2) / nDay)
          // compute difference in days and adjust for day of return
          event.value = Math.abs(nDate1 - nDate2) + 1;
          } // end fields not empty;

           

          It is even possible to not use any if statement by using a logical value as part of the calculation:

           

          // get fields as string values;
          var sDate1=this.getField("Return Date").valueAsString;
          var sDate2=this.getField("Departure Date").valueAsString;
          // convert date strings to date object
          var oDate1 = util.scand("mm/dd/yyyy", sDate1);
          var oDate2 = util.scand("mm/dd/yyyy", sDate2);
          var nDay = 1000 * 60 * 60 * 24;
          // convert date objects to number of days since epoch date and turncate to days
          var nDate1 = Math.floor(Number(oDate1) / nDay)
          var nDate2 = Math.floor(Number(oDate2) / nDay)
          // compute difference in days and adjust for day of return and zero if both dates null;
          event.value = (sDate1 != "" && sDate2 != "") * (Math.abs(nDate1 - nDate2) + 1);

          • 2. Re: If/Else Javascript Request - Date Calculations
            ClemmBallina Level 1

            This was EXACTLY what I needed!! Thank you sooooooo much!