Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Calculating Time

Questions related to using nuBuilder Forte.
Post Reply
treed
Posts: 205
Joined: Mon May 18, 2020 12:02 am
Been thanked: 2 times
Contact:

Calculating Time

Unread post 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.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculating Time

Unread post by kev1n »

What "Input Type (and class)" and "Format" is set for the time field?
treed
Posts: 205
Joined: Mon May 18, 2020 12:02 am
Been thanked: 2 times
Contact:

Re: Calculating Time

Unread post by treed »

Time. No format option available.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculating Time

Unread post 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.
Post Reply