9 Replies Latest reply on Jan 8, 2015 7:30 AM by gkaiseril

    Calculate 24-Hour Time (aka Military Time)


      I am not very good with JavaScript. Having said that I have had someone send me a script to help me with a form that is built in Livecycle ES4, however, it isn't working form me. It works in the HTML page, but I can't get it to work in the Livecycle created form. There are three boxes: "startTime", "endTime" and "totalMin". The user will input data into "startTime" and "endTime" using four digit 24-hour time and the result will be total minutes in the "totalMin" box. (i.e. 0800 - 1700 = 540) This needs to work even if the user crosses midnight (i.e. starts at 2200 and ends at 0300). There will never be an instance where the user will put in time that will exceed 24 hours.


      I have already looked for many examples on the internet, but they seem specific to HTML or PHP and since I am a novice with JS I am having trouble knowing where to make the changes to fit it into liveycle.


      Here is a snippet of the form:



      The fields are labeled as:

      State Time = "startTime"

      End Time = "endTime"

      Minutes Worked = "totalMin"


      I think the issue with the script I was sent was that the values were used to suit his example and I wasn't sure where to make the changes to fit my form. It works brilliantly in the HTML. Here it is:


      <!doctype html>



      <meta charset="utf-8">

      <title>Calculate Minutes</title>

      <script type="application/javascript">

      function calcMinutes() {

        // format military time to HH:MM

        var fromTime = document.getElementById('ft').value.replace(/(\d{2})(\d{2})/,"$1:$2");

        var toTime = document.getElementById('tt').value.replace(/(\d{2})(\d{2})/,"$1:$2");

        // check to see if from time is greater than the to time.  If not we roll to the next day

        var toDay = 1;

        if (toTime < fromTime) {

        toDay = 2;


        // the actual dates aren't important as we are just calculating minutes of difference

        var fromDate = new Date("1/1/2000 " + fromTime);

        var toDate = new Date("1/" + toDay + "/2000 " + toTime);

        // get the difference in milliseconds

        var diffMilliseconds = toDate.getTime() - fromDate.getTime();

        // convert the milliseconds to minutes

        var diffMinutes = diffMilliseconds / 1000 / 60;

        // populate the minutes field

        document.getElementById('min').value = Math.round(diffMinutes);










        <label for="ft">from time:</label>

        <input type="text" name="ft" id="ft"><br><br>

        <label for="tt">to time:</label>

        <input type="text" name="tt" id="tt"><br><br>

        <label for="min">Total Minutes:</label>

        <input type="text" name="min" id="min">



        <input type="button" name="button" id="button" onClick="calcMinutes()" value="Calculate">




        • 1. Re: Calculate 24-Hour Time (aka Military Time)
          gkaiseril MVP & Adobe Community Professional

          Will the shifts start and end on different days?


          I have posted a number of time sheets and scripts that can perform this calculation using JavaScript. Some only require a standard format or the 24 hour military format.


          You bigger challenge will be having a keystroke, formatting, and validation that will accept 2400 or 24:00.


          The following functions should get you started:


          function Time2Num(cTime){
          // convert time string [h]h:MM tt or HH:MM to minutes
          var min = 0
          if ( /(\d{0,2})[:](\d{2})[ ]([ap])/i.test(cTime.toLowerCase()) ) {
             console.println(RegExp.$1 + " " + RegExp.$2 + " " + RegExp.$3);
             // civilian time
             min = Number(RegExp.$1); // get hours;
             // adjust for pm hours 1:00 pm or later
             if(RegExp.$3 == "p" && RegExp.$1 < 12) {
                min = min + 720; // add 12 hours for hours after 12:00 pm;
          // adjsut for 00:00am times
          if(RegExp.$3 == "a" && RegExp.$1 == 12) {
          min = 0; // force hours to o minutes
             min *= 60; // convert hours to minutes
             // add minutes  to computed hours
             min += Number(RegExp.$2);
             } else if(/(\d{0,2})[:](\d{2})/.test(cTime) || /(\d{2})(\d{2})/.test(cTime)) {
                  // civilian & Military 24 hour time format
                  min = 60 * Number(RegExp.$1);
                  min += Number(RegExp.$2);
            return min;
          } // end funciton Time2Num

          function TimeDiff(cEnd, cStart) {
          if(typeof Time2Num != "function") app.alert("Missing Time2Num fucntion", 1, 0);
          var fDiff = 0;
          // compute the difference for first pair of fields
          // get the start time string
          var sStart = this.getField(cStart).valueAsString;
          // get the end time string
          var sEnd = this.getField(cEnd).valueAsString;
          // complete script only if we have data

          if(sStart != '' & sEnd != '') {
          // convert sStart string to seconds
          var fStart = Time2Num(sStart);
          // convert sEnd string to seconds
          var fEnd = Time2Num(sEnd);
          // test for end time before start time
          if(fEnd < fStart) {
          console.println("fEnd: " + fEnd + " " + sEnd);
          console.println("fStart: " + fStart + " " + sStart);
          app.alert("The ending time of " + sEnd + " must be later than the starting time of " + sStart, 1, 0);
          console.println("fEnd: " + fEnd);
          console.println("fStart: " + fStart);
          } else {
          // compute difference in seconds
          fDiff += fEnd - fStart;
          } // end end before start
          } // end non-null entry
          // return difference
          return fDiff;
          } // end TimeDiff

          • 2. Re: Calculate 24-Hour Time (aka Military Time)
            adams6624 Level 1

            Thanks GKaiseril for the reply.


            No, there will not be a time where a someone would want to put in a time to exceed 24 hours. The most time someone would enter would be probably 16 hours.

            Thanks so much for the script. I see that you typed in some "civilian time". This should me just using the 24-hour clock. No AM and no PM. I have it set for the time to be formatted as HHMM. As I said before, I am not real good with Javascript, so when I see the script it is difficult for me to make sense of it. Is this different scripts where I choose the one that best fits my needs or is this the whole script that I need to copy and paste? If the latter, then I don't see the need for the AM and PM, but the rest of your script may take care of that.


            Also, I don't see where I need to make the changes to the fields. My fields are named endTime, startTime and totalMin. I can't tell in your script where I should rename those fields. Thanks so much for the help. Sorry, I am such a novice when it comes to Javascript.


            Lastly, I would think that the script needs to put into the "totalMin" field as 'calculate'. Can you verify that?

            • 3. Re: Calculate 24-Hour Time (aka Military Time)
              gkaiseril MVP & Adobe Community Professional

              My scripts are functions that accept one or more string values as a parameter and return the number of minutes to the calling script. This allows these calculations to be reused multiple times within a form or even copied into other forms and work independently of form field names. The scripts also do not depend on a fixed format, they can accept the meridian time, 24 hour civilian, or 24 hour military time strings. I usually place these scripts at the document level but you can put them in the custom calculation script.


              The TimeDiff functions takes the form field names as parameters and returns the difference in minutes.


              Even with your custom format of "HHMM" Acrobat/Reader will only accept data entered as "HH:MM"  but it will display the military format. If you want the input to be military time, then you need to write custom keystroke, custom format, and custom validation scripts.


              The code to compute the total time using the above function scripts in the "totalMin" field is:


              event.value = TimeDiff("endTime", "startTime");;

              • 4. Re: Calculate 24-Hour Time (aka Military Time)
                adams6624 Level 1

                Thank you so much. That worked perfectly! I just had to change it to run on exit instead of calculate.

                • 5. Re: Calculate 24-Hour Time (aka Military Time)
                  gkaiseril MVP & Adobe Community Professional

                  If the "Minutes Worked" field is read only, the "On blur" action will never occur as there will be no user action exiting the field.

                  • 6. Re: Calculate 24-Hour Time (aka Military Time)
                    adams6624 Level 1

                    The script is being ran in the endTime field on Exit. Thanks again.

                    • 7. Re: Calculate 24-Hour Time (aka Military Time)

                      Hi all,


                      I have used the above script and calculation modified to display in hours and show blank if zero


                      event.value = TimeDiff("endTime", "startTime")/60;

                      if(event.value == 0) event.value = "";;


                      Not sure if that is the best way, however it works.


                      I was just need help with the following


                      How can I adjust the result to reflect:

                      <5  (no change in output)

                      between 5 but <12 (subtract 0.5 from output)

                      for 12 and > (subtract 1 from output)




                      Is there a way to be able to enter 00:00 as a finish time?


                      And to have a end time such as 02:00 for example after a 21:00 start?


                      Your help is much appreciated, cheers

                      • 8. Re: Calculate 24-Hour Time (aka Military Time)
                        Hik5ta Level 1

                        I have partly answered my own question, a bit of reading and trial+error went into this and I am sure there probably a better way but it works.


                        event.value = TimeDiff("finish1mon", "start1mon")/60;

                        if(event.value == 0) event.value = "";

                        else if(event.value <5) event.value = event.value;

                        else if(event.value >=5 && event.value <12) event.value = event.value-.5;

                        else if(event.value >= 12) event.value = event.value-1;

                        I would still love to know however

                        Is there a way to be able to enter 00:00 as a finish time?

                        And to have a end time such as 02:00 for example after a 21:00 start?



                        • 9. Re: Calculate 24-Hour Time (aka Military Time)
                          gkaiseril MVP & Adobe Community Professional

                          You could remove the restriction about the start time after the end time, but you might not like the result.


                          0000 hours is midnight at the start of the day. 0200 hours is 2 hours after the start of the day. So if the start time is0200 hours and one ends at 0000 hours on the same day then 0000 hours =- 0200 hours is an elapsed time of -02:00 hours.


                          The time format for Acrobat will only accept time strings form 0000 hours, 12:00 AM, to 2359.999 hours, 11:59.999. So there is end of day at 0000 hours and 2400 hours is not allowed by the Acrobat format. As stated earlier, you will need to write custom code for keystroke, validation and format.


                          For time periods that involve more than 1 calendar day you need to include the date for the start and end times and then use the JavaScript date object to get the actual time intervals from the Epoch date and the difference in these values will reflect the adjustment for the different days for the start and end times.