Skip to main content
Participating Frequently
Jul 10, 2020 9:38 PM UTC
Answered

Individual Time Calculation

Hi

 

Looking for some help.

 

I found and copied a script to calculate the time spent on a task from 'on' to 'off'. I need this total on each row but I can't figure out why it is causing errors with certain time periods.

I have adjusted the script for each cell, Time 3.0/3.1/3.2 etc
It doesn't matter which row I enter the times in question they always calculate wrong.

 

Any ideas what I am doing wrong? 

 

Angie 

TIA

 

var start = this.getField("Time 3.0").value;
var startArr = start.split(":") ;
var finish = this.getField("Time 4.0").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().lenght == 1)
minDiff = '0' + MinDiff;
var output = hourDiff + "." + minDiff;

event.value = output;

 

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jul 11, 2020 10:07 PM UTC

There are errors in the reply currently marked as correct, too. For example, if one of the fields is empty you'll get weird results. Also, it has a typo (MinDiff instead of minDiff) which will cause an error. I've re-written the code to handle these issues and also added the feature that it can calculate a time difference across midnight.

Use the following:

 

 

var start = this.getField("Time 3.1").valueAsString;
var finish = this.getField("Time 4.1").valueAsString;

if (start=="" || finish=="") event.value = "";
else {
	var startArr = start.split(":") ;
	var finishArr = finish.split(":") ;

	var hourDiff = finishArr[0] - startArr[0];
	var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
	if (minDiff<0) {hourDiff--; minDiff += 100;}
	if (hourDiff<0) hourDiff+=24;
	if (minDiff.toString().length == 1) minDiff = '0' + minDiff;
	event.value = hourDiff + "." + minDiff;
}

 

 

18 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jul 11, 2020 10:07 PM UTC

There are errors in the reply currently marked as correct, too. For example, if one of the fields is empty you'll get weird results. Also, it has a typo (MinDiff instead of minDiff) which will cause an error. I've re-written the code to handle these issues and also added the feature that it can calculate a time difference across midnight.

Use the following:

 

 

var start = this.getField("Time 3.1").valueAsString;
var finish = this.getField("Time 4.1").valueAsString;

if (start=="" || finish=="") event.value = "";
else {
	var startArr = start.split(":") ;
	var finishArr = finish.split(":") ;

	var hourDiff = finishArr[0] - startArr[0];
	var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
	if (minDiff<0) {hourDiff--; minDiff += 100;}
	if (hourDiff<0) hourDiff+=24;
	if (minDiff.toString().length == 1) minDiff = '0' + minDiff;
	event.value = hourDiff + "." + minDiff;
}

 

 

Translate
AnjiKAuthor
Participating Frequently
Jul 11, 2020 10:54 PM UTC

wow, this is awesome... thank you so much. This is now doing everything I asked for.

 

Your time & effort is very much appreciated 🙂 

Translate
Participant
Feb 6, 2022 9:13 AM UTC

can you include the pdf with the script??

Translate
try67
Community Expert
Community Expert
Jul 10, 2020 9:46 PM UTC

Beside that, there are other issues with this code. Where did you find it? People keep using it, even though it's wrong.

I recently posted the correct code on the forum, but it's so annoying to search it...

Translate
try67
Community Expert
Community Expert
Jul 10, 2020 9:50 PM UTC
AnjiKAuthor
Participating Frequently
Jul 10, 2020 11:13 PM UTC

Thank you for the quick reply. I got the script off this site. I will try to find it again & post the link.

 

It's closer but I am still missing something 

 

This is where I am at now

 

var start = this.getField("Time 3.1") .value;
var startArr = start.split(":") ;

var finish = this.getField("Time 4.1") .value;
var finishArr = finish.split(":") ;

var hourDiff = Math.abs(finishArr[0] - startArr[0]) ;
var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
if (minDiff<0) {hourDiff--; minDiff*=-1;}

var output = hourDiff + "." + minDiff;

event.value = output;

 

Thanks 

 

Angie 

Translate
try67
Community Expert
Community Expert
Jul 10, 2020 9:44 PM UTC

Cahnge lenght to length...

Translate