Calculate time difference on subform
Posted: Thu May 28, 2020 2:22 pm
Is it possible to calculate the time difference from the 2 inputs = [time] (hh:mm) fields? Then get the total amount of time.
Yeskev1n wrote:Is it a 24-hour time? (max. 24h ?)
yeskev1n wrote:When should the time be calculated? As soon as a time has been entered?
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();
}
}
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);
}
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);
}