I am not very good with JavaScript. Having said that I have had someone send me a script to help me with a form that is built in Livecycle ES4, however, it isn't working form me. It works in the HTML page, but I can't get it to work in the Livecycle created form. There are three boxes: "startTime", "endTime" and "totalMin". The user will input data into "startTime" and "endTime" using four digit 24-hour time and the result will be total minutes in the "totalMin" box. (i.e. 0800 - 1700 = 540) This needs to work even if the user crosses midnight (i.e. starts at 2200 and ends at 0300). There will never be an instance where the user will put in time that will exceed 24 hours.
I have already looked for many examples on the internet, but they seem specific to HTML or PHP and since I am a novice with JS I am having trouble knowing where to make the changes to fit it into liveycle.
Here is a snippet of the form:
The fields are labeled as:
State Time = "startTime"
End Time = "endTime"
Minutes Worked = "totalMin"
I think the issue with the script I was sent was that the values were used to suit his example and I wasn't sure where to make the changes to fit my form. It works brilliantly in the HTML. Here it is:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Calculate Minutes</title>
<script type="application/javascript">
function calcMinutes() {
// format military time to HH:MM
var fromTime = document.getElementById('ft').value.replace(/(\d{2})(\d{2})/,"$1:$2");
var toTime = document.getElementById('tt').value.replace(/(\d{2})(\d{2})/,"$1:$2");
// check to see if from time is greater than the to time. If not we roll to the next day
var toDay = 1;
if (toTime < fromTime) {
toDay = 2;
}
// the actual dates aren't important as we are just calculating minutes of difference
var fromDate = new Date("1/1/2000 " + fromTime);
var toDate = new Date("1/" + toDay + "/2000 " + toTime);
// get the difference in milliseconds
var diffMilliseconds = toDate.getTime() - fromDate.getTime();
// convert the milliseconds to minutes
var diffMinutes = diffMilliseconds / 1000 / 60;
// populate the minutes field
document.getElementById('min').value = Math.round(diffMinutes);
}
</script>
</head>
<body>
<p>
<label for="ft">from time:</label>
<input type="text" name="ft" id="ft"><br><br>
<label for="tt">to time:</label>
<input type="text" name="tt" id="tt"><br><br>
<label for="min">Total Minutes:</label>
<input type="text" name="min" id="min">
</p>
<p>
<input type="button" name="button" id="button" onClick="calcMinutes()" value="Calculate">
</p>
</body>
</html>