Calculate the duration between 2 Time Pickers - Custom HTML - Javascript

Confluence User - 20 May, 2016

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>	
forms;time;javascript

7


20 May, 2016
confluenceUser
confluenceUser

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>

21 May, 2016
confluenceUser
confluenceUser

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

24 Apr, 2017
confluenceUser
confluenceUser

Where we can write this script ?

24 Apr, 2017
confluenceUser
confluenceUser

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

24 Apr, 2017
confluenceUser
confluenceUser

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 ?

21 Jul, 2017
confluenceUser
confluenceUser

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?

21 Jul, 2017
confluenceUser
confluenceUser

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

RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print