11 Replies Latest reply on Feb 11, 2014 9:32 AM by gkaiseril

    Date field difference calculation

    MaryDisco

      Hi all,

       

      I am new to Adobe and Java scripting so apologies if this has already been answered elsewhere - I have not been able to find it if it has.

      I want to calculate the difference between two dates fields in hours and mins on a form.

      I have two fields, both Date format (dd/mm/yyy HH:MM), a Start and End date and I want the difference between them in the Time format HH:MM.

      Can anyone help me with the script for this? What I have so far is:

       

      var strStart = this.getField("StartTime").value;

      var strEnd = this.getField("EndTime").value;

      if(strStart.length || strEnd.length)

      {

      var dateStart = util.scand("dd/mm/yyyy HH:MM",strStart);

      var dateEnd = util.scand("dd/mm/yyyy HH:MM",strEnd);

      var diff = dateEnd.getTime() - dateStart.getTime();

       

       

      // One Day = (24 hours) x (60 minutes/hour) x

      // (60 seconds/minute) x (1000 milliseconds/second)

      var oneMin = 60 * 60 * 1000;

      var mins = Math.floor(diff/oneMin);

       

       

      event.value = util.printd("HH:MM",mins);

      }

       

      But this is not working...

       

      Mary

        • 1. Re: Date field difference calculation
          try67 MVP & Adobe Community Professional

          But this is not working...

          This is a meaningless statement. What exactly happens? Are there error messages in the JS console? Do you get a result at all? If so, is it wrong? If so, in what way? etc.

          The devil is in the details...

          • 2. Re: Date field difference calculation
            MaryDisco Level 1

            well at the moment I am not getting anything returned into the destination field on my form - no syntax errors are being returned

            • 3. Re: Date field difference calculation
              try67 MVP & Adobe Community Professional

              Open the JS Console (Ctrl+J) and make sure...

              • 4. Re: Date field difference calculation
                MaryDisco Level 1

                Yea - the error I see is:

                TypeError: Invalid argument type.

                Util.printd:16:Field Duration:Calculate

                ===> Parameter oDate.

                • 5. Re: Date field difference calculation
                  try67 MVP & Adobe Community Professional

                  That's what I thought... Your "mins" variable is not a valid representation of a Date object, so it can't be used in the util.printd() method.

                  • 6. Re: Date field difference calculation
                    MaryDisco Level 1

                    ok so if I just set the event.value to mins it returns 0 when I reckon it should be returning the number of minutes

                    • 7. Re: Date field difference calculation
                      try67 MVP & Adobe Community Professional

                      Yes. And converting minutes to HH:MM notation is not complex either. You

                      basically divide it by 60 to get the number of hours, and use the remainder

                      as the number of minutes.

                      • 8. Re: Date field difference calculation
                        gkaiseril MVP & Adobe Community Professional

                        The "util.printd()" method formats a date object using the provided format string to display or print. You are trying to use a number, the difference in hours as the date object. Your difference is the whole hours difference between the end and start times.

                         

                         

                         

                        var strStart = this.getField("StartTime").value;
                        var strEnd = this.getField("EndTime").value;
                        // var strStart = "01/01/2014 09:00"; // debugging test;
                        // var strEnd = "01/01/2014 10:30"; // debugging test;
                        if(strStart.length || strEnd.length) {
                        var dateStart = util.scand("dd/mm/yyyy HH:MM",strStart);
                        var dateEnd = util.scand("dd/mm/yyyy HH:MM",strEnd);
                        var diff = dateEnd.getTime() - dateStart.getTime();
                        // One Day = (24 hours) x (60 minutes/hour) x
                        // (60 seconds/minute) x (1000 milliseconds/second)
                        var oneMin = 60 * 1000;
                        var mins = Math.floor(diff/oneMin);
                        s: " + mins)
                        // get the whole hours;
                        var nHours = Math.floor(mins / 60);
                        // get the minutes less than 1 hour;
                        var nMinutes = mins % 60;
                        // some debugging information;

                        console.show();console.clear(); 

                        console.println("End: " + strEnd);
                        console.println("Start: " + strStart);
                        console.println("Difference minute

                        console.println("Difference hours: " + nHours + " minutes: " + nMinutes);
                        }

                        • 9. Re: Date field difference calculation
                          MaryDisco Level 1

                          thanks - almost there...

                          I have added the bleow:

                          var mins = Math.floor(diff/oneMin);

                          var hrs = Math.floor(mins/60);

                          var rem = mins % 60

                           

                          event.value = ??? - can I string the hrs and rem variables together into the event.value?

                          • 10. Re: Date field difference calculation
                            MaryDisco Level 1

                            Got it - thanks for your help - really appreciate it.

                            • 11. Re: Date field difference calculation
                              gkaiseril MVP & Adobe Community Professional

                              The result can be formatted using:

                               

                              // format result using "h:MM" format

                              event.value = util.printf("%,0 0.0f" + ":" + "%,002.0f", nHours, nMinutes);

                               

                              One cannot use the date or time formats since the time values will be limited to the hours and minutes values for 1 day, so any time value that is over 23 hours 59 minutes is not possible.

                               

                              A full script solution including document level functions for conversion of date strings to minutes and converting minutes to a time string:

                               

                              // reusable document level functions;

                              function Time2Num(cFormat, cDate) {
                              // convert date value with format to number of minutes form Epoch date;
                              var oDate = util.scand(cFormat, cDate);
                              var nMins =  null;
                              if(oDate ==  null) app.alert("Error converting " + cString + " using " + cForamt);
                              else nMins = oDate.getTime() / (1000 * 60);
                              return Math.floor(nMins);
                              } // end Time2Num format

                              function Num2Time(cFormat, nMins) {
                              // convert number of muniutes to h:MM or HH:MM format;
                              // return formatted string for valid formats;
                              // return null for invalid formats;
                              var cElapsed = null;
                              // test for nMins being a number;
                              if(isNaN(nMins)) {
                              app.alert("Minutes must be number",0, 0);
                              } else {
                              var nHours = Math.floor(nMins / 60);
                              var nMinutes = Math.floor(nMins % 60);
                              switch(cFormat) {
                              case "h:MM":
                              cElapsed = util.printf("%,0 0.0f" + ":" + "%,002.0f", nHours, nMinutes);
                              break;
                              case "HH:MM":
                              cElapsed = util.printf("%,002.0f" + ":" + "%,002.0f", nHours, nMinutes);
                              break;
                              default:
                              app.alert("Invalid format " + cFormat + "\nMust be \"HH:MM\" or \"h:MM", 0, 0);
                              break;
                              }
                              }
                              return cElapsed;
                              } // end Num2Time function

                              // end document level funcitons;

                               

                              // custom calculation script;

                              event.value = ''; // clear result;

                              var strStart = this.getField("StartTime").value;
                              var strEnd = this.getField("EndTime").value;

                               

                              if(strStart.length || strEnd.length) {
                              var nDateStart = Time2Num("dd/mm/yyyy HH:MM",strStart);
                              var nDateEnd = Time2Num("dd/mm/yyyy HH:MM",strEnd);
                              var nMins = nDateEnd - nDateStart;
                              // format result using "h:MM" format
                              event.value = Num2Time("h:MM", nMins);
                              }

                              // end custom calculation script;