1
0
-1

Hi Experts,

I want to calculate duration between two times using custom HTML.

 As a input I'm using Time Picker. I have two input fields field1 and field2

 As a result in field field_result I would like to get number of minutes.

Unfortunately no luck, any help would be really appreciated!

<script type="text/javascript">					
    $(document).change(function(){					
        var d1 = "field1";					
        var d2 = "field2;					
        var fieldId = "field_result";					
 					
        //get time values				
        var d3 = $('[name='+d1+']:enabled').val();					
        var d4 = $('[name='+d2+']:enabled').val();					
       
 
// calculate the time   ????
          diff = 0;
if (d3 && d4) {
diff = Math.floor((d4.val() - d3.val()) ); // ms per day
}
          
 					
        //store the value to a field					
        $('[name='+fieldId+']:enabled').val(diff);					
    });					
</script>	
    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Hi,

      I have managed to make it, as the time picker is text field, but I believe code could be further optimized.

       

       

      <script type="text/javascript">
      $(document).click(function(){
      var d1 = "godz_wjazdu_na_platf";
      var d2 = "godz_wyj_z_platf";
      var fieldId = "czas_pobytu";

      //get value from parent form
      var d3 = $('[name='+d1+']:enabled').val();
      var d4 = $('[name='+d2+']:enabled').val();

       

      // calculate number of minutes for first field
      var d5 = d3.substring(0, 2);
      var d6 = d3.substring(3, 5);
      var d7 = ((d5 * 60) + (d6 * 1));


      //calculate number of minutes for second field
      var d8 = d4.substring(0, 2);
      var d9 = d4.substring(3, 5);
      var d10 = ((d8 * 60) + (d9 * 1)) ;

      //final calculation (number of minutes)
      var d11= (d10 - d7) ;

      diff = d11; // ms per day



      //store the value to a field
      $('[name='+fieldId+']:enabled').val(diff);
      });
      </script>

      1. Eric

        great - thanks for posting the solution! I am going to need the same function shortly.

      2. Zehra Batool

        Where we can write this script ?

      3. Eric

        You add a customHTML field to your form and then put the script in there. :)

      4. Zehra Batool

        Thank you for the response. I am new at the joget, could you please explain me in detail where i have to put this script ?? which validator i have to use for this script ?

      5. Izzuddin Pauzi

        What if I want to make sum of d1 and d2, i tried using my own coding but it didn't work. Anyone could help me?

      6. Izzuddin Pauzi

        I want the coding for storing the value to a field like above but without the val(diff)..

      CommentAdd your comment...