3 Replies Latest reply on May 2, 2017 4:02 PM by Karl Heinz Kremer

    If then else not working in PDF

    justing53665679

      Long story short I figured out a simple way to calculate time in PDF using the export value field in the drop down selection list based on 24 hours. I broke the drop downs into three groups Hours (1-12 exports 1-12), Mins (in 15 min chunks based on 1 hr numerical value in export 15=0.25) and the AM/PM (AM 0 or adds 12 hours for PM) for both start and end times along with a break time (1-2 hours in 15 min chunks). The formula works fine for getting the fields, adding them up, and the subtracting the to the the total time worked, even breaking over the midnight barrier (which I had used in another version where the drop down menu contained all 24 hours in 15 min chunks but for this pdf it was too clunky to use the drop down that large). The trouble I noticed is when you use any 12 PM combo with AM time (it creates a midnight crossover because of the math). I then tried to add an else to the if statement doing the midnight crossover and keep getting the error invalid assignment left-hand side after the else if statement. Here's the code I'm trying:

       

      //Get Time Values

      var StartTimeHour = this.getField("STH1").value;

      var StartTimeMin = this.getField("STM1").value;

      var StartTimeAP = this.getField("STAP1").value;

      var EndTimeHour = this.getField("ETH1").value;

      var EndTimeMin = this.getField("ETM1").value;

      var EndTimeAP = this.getField("ETAP1").value;

      var BreakTime = this.getField("Break1").value;

       

      //Combine Time Values

      var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);

      var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);

       

      //Calculate Time

      var TimeWorked = ((EndTime - StartTime) - BreakTime);

       

      //Test for Midnight Crossover

      if(TimeWorked < 0)

      {// Shift 24 hours

      TimeWorked += 24;

      }else if(TimeWorked >0 && EndTimeAP = 12)

      {// Remove 12 hours

      TimeWork -= 12;

      }

      }

       

      //Print Results

      event.value = TimeWorked;

       

      I also tried to put in a separate if statement before the Combine Time Values and got the same error here's that code:

       

      //Get Time Values

      var StartTimeHour = this.getField("STH1").value;

      var StartTimeMin = this.getField("STM1").value;

      var StartTimeAP = this.getField("STAP1").value;

      var EndTimeHour = this.getField("ETH1").value;

      var EndTimeMin = this.getField("ETM1").value;

      var EndTimeAP = this.getField("ETAP1").value;

      var BreakTime = this.getField("Break1").value;

       

      //Test for 12 pm

      if(EndTimeHour = 12 && EndTimeAP = 12)

      {//Remove 12 hours

      EndTimeAP -=12;

      }

       

      //Combine Time Values

      var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);

      var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);

       

      //Calculate Time

      var TimeWorked = ((EndTime - StartTime) - BreakTime);

       

      //Test for Midnight Crossover

      if(TimeWorked < 0)

      {// Shift 24 hours

      TimeWorked += 24;

      }

       

       

      //Print Results

      event.value = TimeWorked;

       

      This is the code that always works but has the 12am/12pm issue I was trying to solve:

       

      //Get Time Values

      var StartTimeHour = this.getField("STH1").value;

      var StartTimeMin = this.getField("STM1").value;

      var StartTimeAP = this.getField("STAP1").value;

      var EndTimeHour = this.getField("ETH1").value;

      var EndTimeMin = this.getField("ETM1").value;

      var EndTimeAP = this.getField("ETAP1").value;

      var BreakTime = this.getField("Break1").value;

       

      //Combine Time Values

      var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);

      var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);

       

      //Calculate Time

      var TimeWorked = ((EndTime - StartTime) - BreakTime);

       

      //Test for Midnight Crossover

      if(TimeWorked < 0)

      {// Shift 24 hours

      TimeWorked += 24;

      }

       

      //Print Results

      event.value = TimeWorked;

       

      I cannot see how the Midnight crossover works here, but when I do something similar right after getting the values I don't understand why that does not work, but the Midnight cross over does. If someone could show me whats wrong that would be great. I tried a non Java fix, by making the ETM1's 12 export out 0, but then this causes a math issue for the midnight crossover.

       

      Thanks

        • 1. Re: If then else not working in PDF
          Karl Heinz Kremer Adobe Community Professional

          You have an extra "}" in your code:

           

          //Test for Midnight Crossover
          if (TimeWorked < 0) { // Shift 24 hours
            TimeWorked += 24;
          } else if (TimeWorked > 0 && EndTimeAP = 12) { // Remove 12 hours
            TimeWork -= 12;
          }
          }
          

           

          The first thing I do when I look at code is to load it into an editor that can reformat JavaScript code. This way, problems like this show up right away. However, you should have gotten a syntax error from Acrobat when you tried to associate this code with your field.

          • 2. Re: If then else not working in PDF
            justing53665679 Level 1

            While I thank you Karl for pointing out the extra "}" in my code, even after downloading a Java editor that had a debugger in it I still had errors. In the end what was causing it was the "EndTimeAP = 12" which was not testing whether EndTimeAP variable equaled 12. After some research I discovered that in Java to test is a variable is equal to something I have to use "==" or "===" if it is zero. In the end this ended up working;

             

            //Test for Midnight Crossover

            if (TimeWorked < 0) { // Shift 24 hours

                 TimeWorked += 24;

            } else if (TimeWorked > 0 && EndTimeAP == 12) { // Remove 12 hours

                 TimeWorked -= 12;

            }

             

            I thanks you for at least pointing out the one error and it helped lead me to this answer.

            • 3. Re: If then else not working in PDF
              Karl Heinz Kremer Adobe Community Professional

              I did not imply that a JavaScript aware editor would find all errors, it just makes finding things like missing braces or parenthesis much easier. What you had was actually not a syntax error, but a logic error because 'a = b' is a valid JavaScript expression.

               

              Glad you figured it out.