JavaScript to auto-calculate man-hours in 24-time for Adobe Form Field?

孤街醉人 提交于 2021-02-08 03:44:27

问题


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.

Example of Input Error

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?

To generate the employee times I use the code below:

UPDATE EDIT THIS IS GENERATING INCORRECT TIMES: Time Calculation Error

// 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 = "";}

To calculate the total site time (total manhours for the specific site) I used this:

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

回答1:


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);



回答2:


This worked, with a tweak for 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 = "";}


来源:https://stackoverflow.com/questions/58261419/javascript-to-auto-calculate-man-hours-in-24-time-for-adobe-form-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!