Page 1 of 2

Calculate time difference on subform

Posted: Thu May 28, 2020 2:22 pm
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.

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 2:36 pm
by kev1n
Is it a 24-hour time? (max. 24h ?)

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 2:47 pm
by kknm
kev1n wrote:Is it a 24-hour time? (max. 24h ?)
Yes

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 2:52 pm
by kev1n
When should the time be calculated? As soon as a time has been entered?

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 3:00 pm
by kknm
kev1n wrote:When should the time be calculated? As soon as a time has been entered?
yes

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 3:32 pm
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();
	}
}

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 3:56 pm
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.

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 5:28 pm
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().

Re: Calculate time difference on subform

Posted: Thu May 28, 2020 5:57 pm
by kev1n
Just tested it - It works for me:
duration.gif
An (updated) code with description can be found in my Code Library.

Re: Calculate time difference on subform

Posted: Fri May 29, 2020 11:56 am
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.