Add 2 duration fields (only hour and minutes) and display result as hh:mm
Hi there,
I'm trying to add up 2 calculation fields and display the results in the format "hh:mm". Can anyone help me with that please?
Thanks a lot.

Rainer Grabowski, I don't know what happened. I created some tests and only one of them calculated right (see print):
The other print is how I put the formula you gave me:

That cause the var m returns in 3 cases a decimal and substr() gets the latest 2 digits of the decimals. I've added one line (another var m) which rounds the result of the first m.
var sum = @H.E. (h) 60% + @H.E. (h) 100%;
var h = parseInt(sum);
var m = 60 * (sum % h);
var m = Math.round(m,0);
("0" + h).substr(2) + ":" + ("0" + m).substr(2)Rainer

Rainer Grabowski. I just tried adding the new line as you told, but it didn't work aswell.
Sorry for bothering you again. XD

Rainer Grabowski, Would you help one more time please? The formula you gave me is working just fine now. But there are some cases that still need to be fixed, for example when the filed used on the calcutation is emty:
It heappens when 1 or both blanks in the Duration field is empty:

Hello Fernando,
great to hear that it works for you. The following code returns
00:00 if both are empty or if only one it filled the hour:min of that one:var he60 = @H.E. (h) 60%  0;
var he100 = @H.E. (h) 100%;  0;
var h = parseInt(sum);
var hx = h == 0 && sum > 0 ? 1 : h;
var m = 60 * (sum % hx)  0;
var m = Math.round(m,0)  0;
var hLen = h.toFixed().length;
var hLen = hLen < 2 ? 2 : hLen;
("0" + h).substr(hLen) + ":" + ("0" + m).substr(2)Rainer

Sorry for the typo and the forgotten sum line:
var he60 = @H.E. (h) 60%  0;
var he100 = @H.E. (h) 100%  0;
var sum = he60 + he100;
var h = parseInt(sum);
var hx = h == 0 && sum > 0 ? 1 : h;
var m = 60 * (sum % hx)  0;
var m = Math.round(m,0)  0;
var hLen = h.toFixed().length;
var hLen = hLen < 2 ? 2 : hLen;
("0" + h).substr(hLen) + ":" + ("0" + m).substr(2)Rainer
Please sign in to leave a comment.
Comments
14 comments