9 Replies Latest reply on Jun 24, 2015 7:23 AM by garrettc871

    Calculate difference between AM and PM time

    garrettc871

      I'm using a form and I want to have the time between two times calculated. That part works - but I'm having problems getting the data after input has been calculated.

      When you enter a time in hh:MM tt format - I have it so that it's calculated and output in HH:MM format already. It doesn't accept that input however - it just tries to use whatever is originally put in the form. This is currently how I'm capturing data from the two fields I need to take data from. I can take this and have people use military time, but I'd prefer to just take input from the hh:MM tt format.

      var hrsStart = parseInt(this.getField("sTime").value.split(":")[0]);

      var minStart = parseInt(this.getField("sTime").value.split(":")[1]);

      var hrsEnd = parseInt(this.getField("stTime").value.split(":")[0]);

      var minEnd = parseInt(this.getField("stTime").value.split(":")[1]);

      This is how I'm calculating data input into the two fields.

      var time = this.getField("stTime");

      var hours = Number(time.match(/^(\d+)/)[1]);

      var minutes = Number(time.match(/:(\d+)/)[1]);

      var AMPM = time.match(/\s(.*)$/)[1].toLowerCase();

       

      if (AMPM == "pm" && hours < 12) hours = hours + 12;

      if (AMPM == "am" && hours == 12) hours = hours - 12;

      var sHours = hours.toString();

      var sMinutes = minutes.toString();

       

      if (hours < 10) sHours = "0" + sHours;

      if (minutes < 10) sMinutes = "0" + sMinutes;


      this.getField("stTime").value = (sHours +':'+sMinutes);

      Is my answer Validation?

      I've also tried to do getHours / getMinutes but the values come up unresolved.

      I will accept any help - even if it's not in line with that solution.  I'm okay with being completely wrong.  =)

        • 1. Re: Calculate difference between AM and PM time
          gkaiseril MVP & Adobe Community Professional

          Have you considered that not only does time need to adjust for am or pm but it may also be affected by the date.

           

          What happens on the day one switches from winter time to summer time for those areas of the world that adjust their clocks for summer or winter time by an hour. So the time following 1:59am becomes 3:00am and the interval between 2:00am and 2:59am does not exist for local time.

           

          Have you looked at using the JavaScript date object, which converts each millisecond of time into numeric value along a number line.

           

          If I were to take a 24 hour format, I would use the civilian 24 hour format since the military is a pure number and JavaScript strips leading zeros unless you force the value to be a string.

          • 2. Re: Calculate difference between AM and PM time
            garrettc871 Level 1

            I have been trying this with the following code:

             

            var strStart = this.getField("sTime1.5").value;

              app.alert(strStart + "   " + strEnd);

            var strEnd = this.getField("stTime1.5").value;

            if(strStart.length && strEnd.length)

            {

              var timeStart = util.scand("h:MM tt", strStart);

              var timeEnd = util.scand("h:MM tt", strEnd);

              app.alert(timeStart + "   " + timeEnd);

              var diff = sTime.getTime() - stTime.getTime();

              var oneMinute = 60 * 1000;

              var timeleft = Math.floor(diff/oneMinute);

              event.value = timeleft;

            }

            else

              event.value = 0;

             

            My first alert returns the value of the input.  My 2nd returns null - what's wrong with my code?

            • 3. Re: Calculate difference between AM and PM time
              try67 MVP & Adobe Community Professional

              Your first alert should not work at all because strEnd is not defined at that moment.

              • 4. Re: Calculate difference between AM and PM time
                try67 MVP & Adobe Community Professional

                Also, just the hour is not enough information to define a Date. You need to also define the day, month and year.

                • 5. Re: Calculate difference between AM and PM time
                  garrettc871 Level 1

                  What would be the proper format? h:MM tt mm/dd/yy?

                   

                  The dates should never be different - so I'm not that worried about that.  If a static date works - that's fine with me.

                   

                  I also noticed that, but it does work for some reason.

                   

                  It works as:

                   

                  var strStart = this.getField("sTime1.5").value;

                  var strEnd = this.getField("stTime1.5").value;

                    app.alert(strStart + "   " + strEnd);

                  if(strStart.length && strEnd.length)

                  {

                    var timeStart = util.scand("h:MM tt m/d/yy", strStart);

                    var timeEnd = util.scand("h:MM tt m/d/yy", strEnd);

                    app.alert(timeStart + "   " + timeEnd);

                    var diff = sTime.getTime() - stTime.getTime();

                    var oneMinute = 60 * 1000;

                    var timeleft = Math.floor(diff/oneMinute);

                    event.value = timeleft;

                  }

                  else

                    event.value = 0;

                   

                  However, I think I need to input the date in the field as well for it to work - and I don't want to do that.  I want just time.  What's the best way to do this?

                  • 6. Re: Calculate difference between AM and PM time
                    try67 MVP & Adobe Community Professional

                    The format doesn't matter as long as it contains those values... Yes, that format should be fine.

                    • 7. Re: Calculate difference between AM and PM time
                      garrettc871 Level 1

                      Is there a way for users to just input like 6:30 am and have any date appended to it?

                      • 8. Re: Calculate difference between AM and PM time
                        try67 MVP & Adobe Community Professional

                        You need to add the date portion as a static string in your code. Something like this:

                        util.scand("h:MM tt m/d/yy", strStart + " 1/1/15");

                        • 9. Re: Calculate difference between AM and PM time
                          garrettc871 Level 1

                          I got it to work: I had tried that before and it didn't work - but I applied the same theory to the vars before they're input.

                           

                          var strStart = this.getField("sTime1.5").value;

                          var strEnd = this.getField("stTime1.5").value;

                          strStart = strStart + " 1/1/70";

                          strEnd = strEnd + " 1/1/70";

                          app.alert(strStart + "  " + strEnd);

                          if(strStart.length && strEnd.length)

                          {

                            var timeStart = util.scand("h:MM tt m/d/yy", strStart);

                            var timeEnd = util.scand("h:MM tt m/d/yy", strEnd);

                            app.alert(timeStart + "  " + timeEnd);

                            var diff = timeEnd - timeStart;

                            var oneHour = 60 * 60 * 1000;

                            var timeleft = (diff/oneHour);

                            event.value = timeleft.toFixed(2);

                          }

                          else

                            event.value = 0;

                           

                          This is the working code for it.