Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Calculating Time
Calculating Time
Hello All, working on a time card app and need to calc the time worked for each day on a sub form. It's desired to have a dynamic calc so that if times are adjusted the hours worked reflects the change. Can someone suggest a strategy to approach this. My attempts to use the calc field have failed. The values in question are stored as Time on MariaDB.
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Calculating Time
The calc object does not support times.
Use a JS function to calculate the sum of the time fields and assign its return value to a (text) object.
Example:
Call the function subformTimeSum() when the form is loaded and in the time's onchange event.
Use a JS function to calculate the sum of the time fields and assign its return value to a (text) object.
Code: Select all
function subformTimeSum(subform, fieldname) {
var totalSeconds = 0;
var a = Array();
var sf = nuSubformObject(subform);
var c = sf.fields.indexOf(fieldname);
for (var i = 0; i < sf.rows.length; i++) {
if (sf.deleted[i] == 0) {
var currentDuration = sf.rows[i][c];
currentDuration = currentDuration.split(":");
var hrs = parseInt(currentDuration[0], 10);
var min = parseInt(currentDuration[1], 10);
var sec = parseInt(currentDuration[2], 10);
var currDurationSec = sec + (60 * min) + (60 * 60 * hrs);
totalSeconds += currDurationSec;
}
}
var hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
var minutes = Math.floor(totalSeconds / 60);
var seconds = totalSeconds % 60;
return hours + ":" + minutes + ":" + seconds;
}
Example:
Code: Select all
var sumTimes = subformTimeSum('subform_object_id', 'time_object_id');
$('#totalTime').val(sumTimes);