Page 1 of 1

Calculating Time

Posted: Wed Sep 01, 2021 9:14 pm
by treed
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.

Re: Calculating Time

Posted: Wed Sep 01, 2021 10:12 pm
by kev1n
What "Input Type (and class)" and "Format" is set for the time field?

Re: Calculating Time

Posted: Wed Sep 01, 2021 10:34 pm
by treed
Time. No format option available.

Re: Calculating Time

Posted: Wed Sep 01, 2021 10:52 pm
by kev1n
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.

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);
Call the function subformTimeSum() when the form is loaded and in the time's onchange event.