Skip to main content
Participating Frequently
June 28, 2020
Question

Form data

  • June 28, 2020
  • 1 reply
  • 1213 views

Hi 

Im looking for a simple java script that will calculate the time in decimal between start / finish time for one week day and also minus a half hour for breaks. The script does not need to account for calendar days as the time frames will always be within the same day.

 

Ive tried several scripts and none seem to work for me. (exp 09:40 to 12:30 = 3.16 and not 2.83 on one script ) The field names i am using are "StartTime" and "FinishTime" The time is 24hr format and I have set the format on the text field to HH:MM.

 

When i have added all the weekdays up in decimal.I would like to convert the decimal back to HH:MM. Appreciate any help

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
Community Expert
June 29, 2020

What script does you use?

Participating Frequently
June 29, 2020

Hi . My apologise I dont have the script to hand as it was one of four that I tried from those published in this community . If think that it may have been this script 

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

// finish
var finish = this.getField("FinishTime").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;
try67
Community Expert
Community Expert
June 29, 2020

The problem is with this line of the code:

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

I don't know why they used 59 instead of 60, for starters. Also, using the absolute value of the difference between the two minutes is incorrect. If you do that you can't know if the result is positive (because the end time's minutes are larger than the start time's minutes) or negative (if they are smaller).