Welcome to the nuBuilder Forums!

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

Calculate time difference on subform

Questions related to customising nuBuilder Forte with JavaScript or PHP.
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Calculate time difference on subform

Unread post by kknm »

Is it possible to calculate the time difference from the 2 inputs = [time] (hh:mm) fields? Then get the total amount of time.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

Is it a 24-hour time? (max. 24h ?)
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Calculate time difference on subform

Unread post by kknm »

kev1n wrote:Is it a 24-hour time? (max. 24h ?)
Yes
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

When should the time be calculated? As soon as a time has been entered?
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Calculate time difference on subform

Unread post by kknm »

kev1n wrote:When should the time be calculated? As soon as a time has been entered?
yes
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

I did it this way once (there might be an easier way but it worked for me)

There are are three text objects with Ids:

time_start
time_end
time_duration

Then attach an onchange event to the objects time_start and time_end and add the JavaScript showDuration();

--> Change the object Ids in the function showDuration()
onchange.png

Code: Select all

function getMinutes(t) {
    var s = t.split(':');
    return Number(s[0]) * 60 + Number(s[1]);
}

// Time difference of two times (format hh:mm) 
function timeDiff(t1, t2) {
    var tMin1 = getMinutes(t1);
    var tMin2 = getMinutes(t2);

    var hour = Math.floor((tMin2 - tMin1) / 60);
    var min = Math.floor((tMin2 - tMin1) % 60);
    return (nuPad2(hour) + ':' + nuPad2(min));
}

function isValidTime(timeString) {
    return (timeString.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/));
}

function validateTimes(t1, t2) {
    if (!isValidTime(t1) || !isValidTime(t2)) {
        return false;
    } else {
        return parseInt(getMinutes(t1)) < parseInt(getMinutes(t2));
    }
}

function showDuration() {
	var t1 = $('#time_start').val();
	var t2 = $('#time_end').val();
	if (validateTimes(t1, t2)) {
	  $( '#time_duration').val(timeDiff(t1,t2)).change();
	} else
	{
	  $('#time_duration').val('').change();
	}
}
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

I've just seen that you want to make the calculation in a subform. I'm going to update my code later to make it work with a subform.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

In a subfrom, it should work like this:

1. Add this JavaScript code to both of your time fields

Code: Select all

timeChanged(event);
2. Add this code to your form's Custom Code field:

Code: Select all

function getMinutes(t) {
    var s = t.split(':');
    return Number(s[0]) * 60 + Number(s[1]);
}

function timeDiff(t1, t2) {
    var tMin1 = getMinutes(t1);
    var tMin2 = getMinutes(t2);

    var hour = Math.floor((tMin2 - tMin1) / 60);
    var min = Math.floor((tMin2 - tMin1) % 60);
    return (nuPad2(hour) + ':' + nuPad2(min));
}

function isValidTime(timeString) {
    return (timeString.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/));
}

function validateTimes(t1, t2) {
    if (!isValidTime(t1) || !isValidTime(t2)) {
        return false;
    } else {
        return parseInt(getMinutes(t1)) < parseInt(getMinutes(t2));
    }
}

function updateDuration(t1, t2, d) {
    if (validateTimes(t1.val(), t2.val())) {
        d.val(timeDiff(t1.val(), t2.val())).change();
    } else {
        d.val('').change();
    }
}

function timeChanged(event) {

    var prefix = $('#' + event.target.id)[0].dataset.nuPrefix;

    var t1 = $("#" + prefix + "time_start");
    var t2 = $("#" + prefix + "time_end");
    var d  = $("#" + prefix + "time_duration");

    updateDuration(t1, t2, d);

}
Rename the object IDs in timeChanged().
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Calculate time difference on subform

Unread post by kev1n »

Just tested it - It works for me:
duration.gif
An (updated) code with description can be found in my Code Library.
You do not have the required permissions to view the files attached to this post.
kknm
Posts: 366
Joined: Sat Apr 11, 2020 12:03 am
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Calculate time difference on subform

Unread post by kknm »

Sorry for the long pause.
Your code works fine in both form-edit and subform, even when using input = Time (Varchar (5))
My task conditions have changed. Since I am not strong in js, for a long time I tried to remake your code to fit my needs.
They are:
Pro_num - Device No. (1, 2)
Pro_beg - start
Pro_end - end
Pro_dif - difference for number 1
Pro_diff - difference for number 2

Code: Select all

if (nuFormType() == 'edit') {
 
       var f = nuSubformObject("").fields;          // include all fields of your main form.
//        custFieldLabelsOnTop(f, []);
        custFieldLabelsOnTop($.merge(f,['sub_vupusk','sub_muvupusk','sub_hodki','sub_otsev','sub_prostoy']), []);

} 

function getMinutes(t) {
    var s = t.split(':');
    return Number(s[0]) * 60 + Number(s[1]);
}

function timeDiff(t1, t2) {
    var tMin1 = getMinutes(t1);
    var tMin2 = getMinutes(t2);

    var hour = Math.floor((tMin2 - tMin1) / 60);
    var min = Math.floor((tMin2 - tMin1) % 60);
    return (nuPad2(hour) + ':' + nuPad2(min));
}

function isValidTime(timeString) {
    return (timeString.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/));
}

function validateTimes(t1, t2) {
    if (!isValidTime(t1) || !isValidTime(t2)) {
        return false;
    } else {
        return parseInt(getMinutes(t1)) < parseInt(getMinutes(t2));
    }
}

function updateDuration(t1, t2, d, a, e) {
    if (validateTimes(t1.val(), t2.val())) {
        
        if (a == '1') {
            
            d.val(timeDiff(t1.val(), t2.val())).change();
            e.val('').change();
       
        } else {
            e.val(timeDiff(t1.val(), t2.val())).change();
            d.val('').change();            
        }    
        
    } else {
        d.val('').change();
        e.val('').change();        
    } 
}

function timeChanged(event) {

    var prefix = $('#' + event.target.id)[0].dataset.nuPrefix;

    var t1 = $("#" + prefix + "pro_beg");
    var t2 = $("#" + prefix + "pro_end");
    var d  = $("#" + prefix + "pro_dif");
    var e  = $("#" + prefix + "pro_diff");
    var a  = $("#" + prefix + "pro_num");
    
    updateDuration(t1, t2, d, e, a);

}
js.gif
My problem is that the result falls into pro_num, not pro_dif or pro_diff.
You do not have the required permissions to view the files attached to this post.
Post Reply