4 Replies Latest reply on May 31, 2011 11:37 AM by treysgrn

    Calculate only if both variables exist

    treysgrn

       

      I have searched high and low for a solution and have yet to find one that works for my calculation.

       

      Here is the code I currently have in the field doing the calculation:

       

       

       

      var oneDay = 24*60*60*1000;

       

      var firstDate = new Date(rentalbegin.rawValue);

      var secondDate = new Date(rentalend.rawValue);

      Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

       

       

       

      This calculates the difference between two dates to determins the total amount of rental days. The problem is, when only one variable is entered the calculation reads the empty field as 0 and calculates the difference. I need something that says 'if rentalend.rawValue == 0 then event.value = "" '. I appreciate any help in this matter.

       

      Thank you,

      Trey

        • 1. Re: Calculate only if both variables exist
          gkaiseril MVP & Adobe Community Professional

          You need to use the 'if' statement to see if a date has been entered for both dates and then perform the calculation or clear the field.

           

          var oneDay = 24*60*60*1000;

           

          if(rentalbegin.rawValue != "" & rentalend.rawValue != "") {

          var firstDate = new Date(rentalbegin.rawValue);

          var secondDate = new Date(rentalend.rawValue);

          Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

          } else {

          "";

          }

           

          You might find FormCalc an easier language to perform this calculation as the 'Date2Num' function returns the number of days since the Epoch date and not the number of milliseconds and has the "HasValue()" function.

          1 person found this helpful
          • 2. Re: Calculate only if both variables exist
            treysgrn Level 1

            What would be the formcalc equivalent code?

            • 3. Re: Calculate only if both variables exist
              treysgrn Level 1

              Also, the above if else statement still produces the same effect.

              • 4. Re: Calculate only if both variables exist
                treysgrn Level 1

                I fixed the statement by figuring out what the value of the rentalend field was. In the code it was if rentalend.rawValue != "". Well the value was being calculated somewhere or else I wouldnt have been getting a result. So I changed the code to reflect " if(rentalend.rawValue > "1") " and it worked! Thank you so much!

                 

                Here is the full working code for those that need it as well.

                 

                 

                 

                var oneDay = 24*60*60*1000;

                 

                if(rentalend.rawValue > "1") {

                 

                var firstDate = new Date(rentalbegin.rawValue);

                var secondDate = new Date(rentalend.rawValue);

                Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

                }

                else {

                "";

                }