Skip to main content
joshuab76782683
Participating Frequently
March 21, 2019
Answered

Difference Between Start time and Finish time in Form

  • March 21, 2019
  • 1 reply
  • 611 views

Hi Guys,

I currently working on a timesheet that Calculates the difference between start and end time.

this is the code I have so far.

// start
var start = this.getField("StartRow1").value;
var startArr = start.split(":");

// finish
var finish = this.getField("FinishRow1").value;
var finishArr = finish.split(":");

// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 59)*100);

if (minDiff.toString().length == 1)
    minDiff = '0' + minDiff;

var output = hourDiff + "." + minDiff;
event.value = output;

the problem is that when no start and end time is entered into the columns I get a 0.NaN. I don't want this to show. I want to total hours cell to be blank if I have not inserted any start and finish times. at the moment the table below represents what the code is giving me. I need a script that will give an empty cell if nothing in inserted.

thanks

StartEndTotal Hours
9:0017:008.00
0.NaN
0.NaN
This topic has been closed for replies.
Correct answer try67

Change the start of your code to:

var start = this.getField("StartRow1").valueAsString;

var finish = this.getField("FinishRow1").valueAsString;

if (start=="" || finish=="") event.value = "";

else {

    // put the rest of your code here

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 21, 2019

Change the start of your code to:

var start = this.getField("StartRow1").valueAsString;

var finish = this.getField("FinishRow1").valueAsString;

if (start=="" || finish=="") event.value = "";

else {

    // put the rest of your code here

}