Skip to main content
Known Participant
Jul 6, 2022 2:37 PM UTC
Answered

Timesheet help

Hi All 

I'm trying to create an auto calculated timesheet and I got it to work if the input time starts and end on the same day using this code:

var start = this.getField("Start Time").value;
var startArr = start.split(":");
var finish = this.getField("Stop Time").value;
var finishArr = finish.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

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

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

 

I ran into a problem where if the Stop Time happens the next day (or 3 days later, etc) how do I go about calculating that? 

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jul 6, 2022 6:47 PM UTC

You could use the util.scand method to convert the values into Date objects. Then use the getTime method of each one of those objects to get a representation of that time as a number. Deduce the Stop Time number from the Start Time number and you got your results (in milliseconds). Then convert it to hours and you're done.

10 replies

try67
Community Expert
Community Expert
Jul 6, 2022 3:36 PM UTC

The script has no way of knowing if the time elapsed between 8:00 and 9:00 is one hour, 25 hours, 49 hours, etc., unless you also specify the full date for each time.

Translate
Linh5FC5Author
Known Participant
Jul 6, 2022 6:01 PM UTC

so if I add in a time and date field like this, do you have a suggestion as to how to start figuring out a way to do it? Thanks

 

getField("Start Time").value = util.printd("dd-mmm-yyyy HH:MM", new Date());

getField("Stop Time").value = util.printd("dd-mmm-yyyy HH:MM", new Date());

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jul 6, 2022 6:47 PM UTC

You could use the util.scand method to convert the values into Date objects. Then use the getTime method of each one of those objects to get a representation of that time as a number. Deduce the Stop Time number from the Start Time number and you got your results (in milliseconds). Then convert it to hours and you're done.

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 6, 2022 3:15 PM UTC

Add fields for the dates.

Translate