8 Replies Latest reply on May 24, 2009 1:31 PM by DianeMarieDennis

    Custom java script needed for format "time" to add and give answer in two decimal number format

    DianeMarieDennis Level 1

      Hello!

       

      I am creating a time sheet and I need help with creating a custom java script.

       

      The end user wants to be able to enter hours on a job in a "time" format, and then have those hours add up and display in a two decimal number format.

       

      For example:

       

      Time in:     8:15  (field name is Text5 and is formatted as a "time" format HH:MM)

      Time out: 14:30  (field name is Text6 and is formatted as a "time" format HH:MM)

      Time in:   14:45  (field name is Text26 and is formatted as a "time" format HH:MM)

      Time out: 14:75  (field name is Text27 and is formatted as a "time" format HH:MM)

      Sub-total:  9.25  (field name is Text28  and is formatted as a two-decimal "number" format)

       

      When I try the obvious (or what to me seems obvious) formula in field Text28:

       

      var v1 = 1 * this.getField("Text5").value;
      var v2 = 1 * this.getField("Text6").value;
      var v3 = 1 * this.getField("Text26").value;
      var v4 = 1 * this.getField("Text27").value;
      event.value = v2 - v1 + v4 - v3;

       

      I get the following message when entering values in fields Text5, Text6, Text26 and Text27:

       

        The value entered does not match the format of the field [ Text28 ]

       

      and the field Text28 does not calculate.


      Is there a custom script that will do what I need it to do, and if so can someone please tell me what it is?

       

      Thank you SO MUCH for any help anyone can give me. =)

       

      Have a great day!

      Diane

        • 1. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
          DianeMarieDennis Level 1

          Well, I do believe I have solved my problem with the help of another post here at the forums.

           

          The post is here: http://forums.adobe.com/message/1099837#1099837

           

          and I modified the script that is posted there so that it looks like this:

           

           

          var oStart = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text5").value);

           

          var oEnd = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text6").value);

           

          var DiffHours = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;

           

          var oStart = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text26").value);

           

          var oEnd = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text27").value);

           

          var DiffHours1 = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;

           

          this.getField("Text28").value = Number(DiffHours + DiffHours1);

           

           

          So far it seems to be working just fine so THANK YOU to sgeorgeaz for posting the formula you created that I was able to modify to work for me.

           

          Everyone have an outstanding day and weekend!

          Diane Dennis

          • 2. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
            DianeMarieDennis Level 1

            Well unfortunately I jumped the gun and my problem is not solved. =(

             

            I used the script that I described above and it does do the calculation as I need it to however, every time I enter something in another field, whether it be a "time" field or a "none" field, I get the following messages:

             

            The value entered does not match the format of the field [ Text10 ]

             

            Click OK

             

            The value entered does not match the format of the field [ Text24 ]

             

            Click OK

             

            The value entered does not match the format of the field [ Text31 ]

             

            Click OK

             

            The value entered does not match the format of the field [ Text38 ]

             

            Click OK

             

            The value entered does not match the format of the field [ Text45 ]

             

            Click OK

             

            Suddenly 0.00 appears in Text17 and Text52

             

            As I delete the zeros that appear then I get the same error messages above and then zeroes start appearing in the other Subtotal fields

             

            When I try to "debug" the javascript I get the following message:

             

            Acrobat EScript Built-in Functions Version 9.0
            Acrobat Annotations / Collaboration Built-in Functions Version 9.0
            Acrobat Annotations / Collaboration Built-in Wizard Functions Version 9.0
            Acrobat SOAP 9.0

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text24:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text31:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text38:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text31:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text10:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text24:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text31:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text38:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text52:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text45:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text10:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text38:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text38:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text10:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text45:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text10:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text24:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text31:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text45:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text52:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text38:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text31:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text24:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text45:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text52:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text17:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.
            Field.value:13:Field Text52:Calculate

             

            I tried changing the work Text to Field (ie: Text52 to Field52) but I still get the same messages.

             

            Therefore I have removed the calculation script from the boxes.

             

            Can someone please help me?

             

            I'm using Acrobat 9 Pro on my Macintosh 10.5.5 but the end result user is using a Windows computer.

             

            Thank you again!

            Diane Dennis

            • 3. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
              gkaiseril MVP & Adobe Community Professional

              "The value entered does not match the format of the field [ Text10 ]" means the value you are trying to display does not match the format you assigned to the "Text10" field. Try changing the format to "None" for that field and see what result you get for that field.  The answer is in hours and decimal hour not hours:minutes. Your "Text5" field should be formatted as a time field in 24 hour format.

               

              If you are going to try a complex calculation, you might try it on a smaller scale, like the minimum number of fields for 1 calculation.

               

              Providing a more meaningful name for you form fields would also help.

               

              You could also add some code to provide information about what your script is doing.

               

              console.show(); // open the JavaScirpt console

              console.clear(); // clear the JavaScript console

              console.println("Text5 value: " + this.getField("Text5").value); // show my input value

              var oStart = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text5").value);

              console.println("oStart object: " + oStart); // see if we have a conversion

               

              console.println("Text6 value: " + this.getField("Text6").value); // show my input value

              var oEnd = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date()) + this.getField("Text6").value);

              console.println("oEnd object: " + oEnd); // see if we have a conversion

               

              var DiffHours = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;

              console.println("Difference in Hours: " + DiffHours); // print the result of the calculation


              Since you are repeating this calculation, you might consider using a function that would take your time string as input and return the hours and minutes since the start of the day as a decimal number.

              • 4. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
                DianeMarieDennis Level 1

                Thank you very much!

                 

                I will try to decipher what you have written and see if I can follow your

                instructions. =)

                 

                Thank you again and have a great day and weekend!

                Diane

                --

                Diane Dennis

                866-480-7105 fax

                http://www.thecontractorsgroup.com

                http://www.aspergers-and-pdd.com/thecontractorsgroup.html

                http://www.explore-whidbey-island.com

                • 5. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
                  gkaiseril MVP & Adobe Community Professional

                  Try the following script in Acrobat's JavaScript  console and to see the diffeence between the decimal and HH:MM formats.

                   

                  function Time2Num(sFormat, sTime) {
                  /*
                  convert time string (sTime) with format of sFormat
                  to the seconds since the start of the day
                  */
                  if(sTime == '') return ''; // exit
                  // get date time for Epoch date and sTime
                  var oTime = util.scand('mm/dd/yyyy ' + sFormat, '01/01/1970 ' + sTime);
                  // convert UTC Offset to milliseonds for adjustment
                  var fTZOffset = oTime.getTimezoneOffset() * 1000 * 60
                  // time since the start of the day in millseconds
                  var fTime = oTime.valueOf() - fTZOffset;
                  // convert to seconds and return value
                  return Math.round(fTime / 1000);
                  }

                  function Num2sTime(fValue) {
                  // convert minutes to HH:MM string
                  fDiff = Math.round(Number(fValue) / 60);
                  // get hours from fDiff
                  fHours = Math.floor(fDiff / 60);
                  // get minutes less than hour from fDiff
                  fMins = Math.round(((fDiff / 60) - fHours ) * 60);
                  // format fMins with leading zero
                  sMins = util.printf("%,302.0f", fMins);
                  // build display string
                  return fHours + ':' + sMins;
                  }

                  // get the start time
                  var sStart = "09:00";
                  // get the end time
                  var sEnd = "17:30";
                  // complete script only if we have data
                  if(sStart != '' & sEnd != '') {
                  // convert sStart string to seconds
                  var fStart = Time2Num('hh:mm', sStart);
                  // convert sEnd string to seconds
                  var fEnd = Time2Num('hh:mm', sEnd);
                  // compute difference in seconds
                  var fDiff = fEnd - fStart;
                  // convert to rounded minutes
                  fDiff = Math.round(fDiff / 60);

                  // compute decimal hours
                  var dDiff = fDiff / 60;
                  // report decimal hours
                  console.println('fDiff as decimal value: ' + dDiff + ' type of: ' + (typeof dDiff) );

                  // report as HH:MM
                  // get hours from fDiff
                  fHours = Math.floor(fDiff / 60);
                  // get minutes less than hour from fDiff
                  fMins = ((fDiff / 60) - fHours ) * 60;
                  // format fMins with leading zero
                  sMins = util.printf('%,302.0f', fMins);
                  // build display string
                  var sDiff = fHours + ':' + sMins;
                  console.println('fDiff as HH:MM : ' + sDiff + ' type of: ' + (typeof sDiff) );
                  }

                   

                  Another thing to consider is that the "Time" format of "HH:MM" only will allow values between 0:00 and 23:59 since these values are the hours and minutes within one day.

                   

                  One also has to be careful and aware the the numer 9 can be used as a number or stirng but "9:00" is a character string and not a nubmer but 540 is the number of minutes to 9:00 am.

                  • 6. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
                    DianeMarieDennis Level 1

                    Hi!

                     

                    Unfortunately it didn't do anything, the total field stayed blank.

                     

                    I've made some changes to the text field names but not descriptive as you

                    suggested, simply to keep it simple.

                     

                    Now it's:

                     

                    Time In:   8:15 (named Field5 and coded "Time" HH:MM)

                    Time Out: 14:30 (named Field6 and coded "Time" HH:MM)

                    Time In:  14:75 (named Field8 and coded "Time" HH:MM)

                    Time Out: 17:75 (named Field9 and coded "Time" HH:MM)

                    Subtotal:  9.25 (named Field10 and coded "Number" with two decimals)

                     

                    The sad thing is, the original script that I found and modified to include

                    two sets of Time In/Time Out worked, it subtotaled the times exactly the way

                    I need, but it kept giving those error messages.  This is the script, if I

                    could figure out how to modify it so that it wouldn't give off those error

                    messages then I'd be good to go:

                     

                    var oStart = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new

                    Date()) + this.getField("Field5").value);

                     

                    var oEnd = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date())

                    + this.getField("Field6").value);

                     

                    var DiffHours = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;

                     

                    var oStart = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new

                    Date()) + this.getField("Field8").value);

                     

                    var oEnd = util.scand("mm/dd/yy hh:mm", util.printd("mm/dd/yy ", new Date())

                    + this.getField("Field9").value);

                     

                    var DiffHours1 = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;

                     

                    this.getField("Field10").value = Number(DiffHours + DiffHours1);

                     

                    Thank you much for trying (did you write that script yourself and if so

                    thank you very much for the time you spent on it), I hope that someone can

                    help me to get this to work.

                     

                    Thank you again and have a great weekend!

                    Diane Dennis

                    http://www.thecontractorsgroup.com

                    • 7. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
                      gkaiseril MVP & Adobe Community Professional

                      "InvalidSetError: Set not possible, invalid or unknown.
                      Field.value:13:Field Text24:Calculate"

                      This is a debugging error. The field with the error is "Text24" and the error is occurring in the calculation script at line 13 and for some reason your request to set a value is not possible. This could be caused by the way you are referencing the field. It appears that this is the same problem for most of the fields. With out seeing the calculation script, there is no way to diagnose this problem. If all of the fields are using the script with a change in a field name, a document level function might be more useful because this will limit the error to one block of code, will make coding the individual fields easier, make maintenance easier as there will only be one place the code needs to be changed, the PDF should be smaller and run faster.

                      • 8. Re: Custom java script needed for format "time" to add and give answer in two decimal number format
                        DianeMarieDennis Level 1

                        Hi!

                         

                        Thank you again for your time!

                         

                        You wrote "without seeing the script" but, and I'm asking this out of pure ignorance (I am so out of my realm here), isn't what I posted above "the script"?

                         

                        Is it possible to attach the document to a post so that you can see it, or can I send you the document somehow?

                         

                        Thank you again!

                        Diane