问题描述
我管理着一个景观工作人员,而不是手动填写表格,我想通过手机来完成,并在表格字段中自动计算时间.我不熟悉JavaScript,需要一些帮助来获取正确的代码,以便计算乘员时间和站点总工时无,当我更改此错误时时代.注意:我将使用24小时制.
I run a landscape crew and instead of filling out our forms manually I would like to do it via cellphone and have the times auto-calculated in the form fields. I'm not familiar with JavaScript and need some assistance in getting the correct code in order to calculate the crew times and total site man-hours without this error when I change the times. Note: I will use 24-hour time.
我尝试了一些发现的不同JavaScript片段,尽管它们起作用,但在操纵时间输入时却出现格式错误.关于如何将此脚本编写到Adobe中的任何建议?
I tried a few different JavaScript snippets I discovered and though they work I am getting a format error when manipulating the time input. Any suggestions on how to script this into 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
推荐答案
您可以计算两个Javascript日期对象之间的经过时间(以毫秒为单位),如下所示:
You can calculate the elapsed time between two Javascript Date objects in milliseconds like this:
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
或者如果您想将小时数返回为小数.
Or if you want to return hours as decimal.
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
我猜测您可以执行以下操作:
I'm guessing that you can then do something like this:
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);
这篇关于JavaScript是否可以自动计算Adobe Form Field的24小时工时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!