JavaScript 自动计算 Adobe Form Field 的 24 小时工时?
JavaScript to auto-calculate man-hours in 24-time for Adobe Form Field?
我 运行 一个景观工作人员,而不是手动填写我们的表格,我想通过手机完成,并在表格字段中自动计算时间。我 不 熟悉 JavaScript,需要一些帮助来获取正确的代码,以便计算工作人员时间和站点总工时 without 改时间的时候出现这个错误。 注意:我会使用24小时制。
我尝试了几个不同的 JavaScript 片段,虽然它们有效,但我在处理时间输入时遇到格式错误。关于如何将其编写到 Adobe 中的任何建议?
要生成员工时间,我使用以下代码:
更新编辑这是生成错误的时间:
// start
var start = this.getField("Monday Site #1 Start Time").value;
var startArr = start.split(":");
// finish
var finish = this.getField("Monday Site #1 Depart Time").value;
var finishArr = finish.split(":");
// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);
if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;
var output = hourDiff + "." + minDiff;
event.value = output;
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {
event.value = "";}
为了计算总站点时间(特定站点的总工时)我使用了这个:
var t1 = this.getField("WS1 Total").value;
var t2 = this.getField("WS1 Total").value;
var t3 = this.getField("WS1 Total").value;
event.value = t1+t2+t3
您可以像这样在毫秒内 calculate the elapsed time between two Javascript Date objects:
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);
return hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
console.log(timeDiff('6:24', '8:13')) // 1:49
或者如果您想要 return 小时作为十进制。
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = diff / 1000 / 60 / 60;
return hours.toFixed(2)
}
console.log(timeDiff('6:24', '8:13')) // 1.82
我猜你可以这样做:
var finish = this.getField("Monday Site #1 Depart Time").value;
var start = this.getField("Monday Site #1 Start Time").value;
this.getField("MS1T").value = timeDiff(start, finish);
这有效,对 NaN 进行了调整:
var start = this.getField("Monday Site #1 Depart Time").value;
var finish = this.getField("Monday Site #2 Depart Time").value;
this.getField("MS1 Total").value = timeDiff(start, finish);
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {event.value = "";}
我 运行 一个景观工作人员,而不是手动填写我们的表格,我想通过手机完成,并在表格字段中自动计算时间。我 不 熟悉 JavaScript,需要一些帮助来获取正确的代码,以便计算工作人员时间和站点总工时 without 改时间的时候出现这个错误。 注意:我会使用24小时制。
我尝试了几个不同的 JavaScript 片段,虽然它们有效,但我在处理时间输入时遇到格式错误。关于如何将其编写到 Adobe 中的任何建议?
要生成员工时间,我使用以下代码:
更新编辑这是生成错误的时间:
// start
var start = this.getField("Monday Site #1 Start Time").value;
var startArr = start.split(":");
// finish
var finish = this.getField("Monday Site #1 Depart Time").value;
var finishArr = finish.split(":");
// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);
if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;
var output = hourDiff + "." + minDiff;
event.value = output;
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {
event.value = "";}
为了计算总站点时间(特定站点的总工时)我使用了这个:
var t1 = this.getField("WS1 Total").value;
var t2 = this.getField("WS1 Total").value;
var t3 = this.getField("WS1 Total").value;
event.value = t1+t2+t3
您可以像这样在毫秒内 calculate the elapsed time between two Javascript Date objects:
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);
return hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
console.log(timeDiff('6:24', '8:13')) // 1:49
或者如果您想要 return 小时作为十进制。
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = diff / 1000 / 60 / 60;
return hours.toFixed(2)
}
console.log(timeDiff('6:24', '8:13')) // 1.82
我猜你可以这样做:
var finish = this.getField("Monday Site #1 Depart Time").value;
var start = this.getField("Monday Site #1 Start Time").value;
this.getField("MS1T").value = timeDiff(start, finish);
这有效,对 NaN 进行了调整:
var start = this.getField("Monday Site #1 Depart Time").value;
var finish = this.getField("Monday Site #2 Depart Time").value;
this.getField("MS1 Total").value = timeDiff(start, finish);
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {event.value = "";}