Calculating Time

Questions related to using nuBuilder Forte.

Calculating Time

Unread postby treed » Thu Sep 02, 2021 4:44 am

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.
treed
 
Posts: 97
Joined: Mon May 18, 2020 7:32 am

Re: Calculating Time

Unread postby kev1n » Thu Sep 02, 2021 5:42 am

What "Input Type (and class)" and "Format" is set for the time field?
kev1n
nuBuilder Team
 
Posts: 2008
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Calculating Time

Unread postby treed » Thu Sep 02, 2021 6:04 am

Time. No format option available.
treed
 
Posts: 97
Joined: Mon May 18, 2020 7:32 am

Re: Calculating Time

Unread postby kev1n » Thu Sep 02, 2021 6:22 am

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.
kev1n
nuBuilder Team
 
Posts: 2008
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5


Return to General

Who is online

Users browsing this forum: No registered users and 18 guests

cron