You are viewing limited content. For full access, please sign in.

Question

Question

Forms 10.2 Time field calculations

asked on March 1, 2017

I am currently using 2 time fields and a jquery script to find the difference between the two. 

 function timediff() {
    var end_time = $('.EndTime input').val();
    var start_time = $('.StartTime input').val();
    var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;
    $(".JavaNum input").val(diff);
  }
  $('.EndTime').on('change', function () {
  timediff();
  });

I am now trying to do the same thing but in a table: 

However the code I am using doesn't seem to working. Any input on what I could possibly be doing wrong here?

$(document).ready(function () {
  
  $('.cf-table-block').on('blur', 'input', tablediff);
 
  function tablediff() {
    $('.cf-table-block tbody tr').each(function () {
      var end_time = $(this).find('timeout input').val();
      var start_time = $(this).find('timein input').val();
      var interrupt = $(this).find('interupt input').val();
      var totaltime = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;
      $(this).find('totalhour input').val(totaltime);
      });
  }
  
});

 

0 0

Answer

SELECTED ANSWER
replied on March 1, 2017

Hi Shane,

For the "timeout", "timein", "interrupt" and "totalhour", are these CSS classes you added to fields?

If so, you need to find field by ".classname"; find('timeout') is used to find element with tag name "timeout".

So the script should look like this:

$(document).ready(function () {  
  $('.cf-table-block').on('blur', 'input', tablediff); 
  function tablediff() {
    $('.cf-table-block tbody tr').each(function () {
      var end_time = $(this).find('.timeout input').val();
      var start_time = $(this).find('.timein input').val();
      var interrupt = $(this).find('.interupt input').val();
      var totaltime = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;
      $(this).find('.totalhour input').val(totaltime);
      });
  }  
});

 

0 0
replied on March 2, 2017

I don't know how I missed that. Thank You!

 

0 0

Replies

replied on January 23, 2018

Instead of using the JavaScript to calculate the time difference, you can use the built-in formula to calculate the difference as well(supported since Forms 10.3): 

Two time fields: start_time 01:00:00 PM,   end_time 04:00:00 PM

Number field with following formula:

= (end_time-start_time)*24

will get 3;

= Sub(end,start)*24 

will get 3.

0 0
replied on December 18, 2018

Does this not work for table cells? I couldn't get the following to work:
= (table_name.end_time(ROW())-table_name.start_time(ROW()))*24

where end_time and start_time are Time fields and columns in a table variable, table_name

0 0
replied on December 21, 2018

It should work for table cells but I think you need '=INDEX(...)' to get the actual value

1 0
replied on June 30, 2020

Xiuhong,

 

How would this work inside a collection?

I have a labor section setup on my form as a collection: Time in, time out, labor performed, and I need it to calculate the hours. 

 

Regards,

Travis

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.