Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Calculate time difference on subform
-
- Posts: 366
- Joined: Sat Apr 11, 2020 12:03 am
- Has thanked: 3 times
- Been thanked: 4 times
- Contact:
Calculate time difference on subform
Is it possible to calculate the time difference from the 2 inputs = [time] (hh:mm) fields? Then get the total amount of time.
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
-
- 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
When should the time be calculated? As soon as a time has been entered?
-
- 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
yeskev1n wrote:When should the time be calculated? As soon as a time has been entered?
-
- 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
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()
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()
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.
-
- 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
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.
-
- 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
In a subfrom, it should work like this:
1. Add this JavaScript code to both of your time fields
2. Add this code to your form's Custom Code field:
Rename the object IDs in timeChanged().
1. Add this JavaScript code to both of your time fields
Code: Select all
timeChanged(event);
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);
}
-
- 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
Just tested it - It works for me:
An (updated) code with description can be found in my Code Library.
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.
-
- 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
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
My problem is that the result falls into pro_num, not pro_dif or pro_diff.
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);
}
You do not have the required permissions to view the files attached to this post.