Hello @Alexander-12
I hope you are doing well, and thanks for reaching out. We’re sorry for the trouble you had.
Please note that the following suggestions may or may not work, as the examples are a sample script and may need changes as per the current environment.
Observations and suggestions:
What's going wrong
- Time fields store strings, not numbers. A field formatted as Time holds a value like
"08:00 AM". Doing event.value / 60 on a string returns NaN, which is why your final result never comes out right. - The format token
"h:mm tt" is wrong. Per Adobe's JavaScript for Acrobat API Reference, lowercase mm means month, not minutes. Minutes must be uppercase: "h:MM tt". Adobe uses this exact token in its own helpx examples (e.g., the dynamic-stamp date/time tutorial). - No subtraction is being done. You need to parse each time into a Date object with
util.scand, subtract, then convert milliseconds to decimal hours.
Rename the four field names to match yours.
(function () {
var sIn = this.getField("TimeIn").valueAsString;
var sLunchOut = this.getField("LunchOut").valueAsString;
var sLunchIn = this.getField("LunchIn").valueAsString;
var sOut = this.getField("TimeOut").valueAsString;
// If any field is empty, clear Total and stop
if (sIn === "" || sLunchOut === "" || sLunchIn === "" || sOut === "") {
event.value = "";
return;
}
// "h:MM tt" — uppercase MM = minutes (lowercase mm would be month)
var fmt = "h:MM tt";
var dIn = util.scand(fmt, sIn);
var dLunchOut = util.scand(fmt, sLunchOut);
var dLunchIn = util.scand(fmt, sLunchIn);
var dOut = util.scand(fmt, sOut);
// util.scand returns null if parsing fails — handle gracefully
if (dIn == null || dLunchOut == null || dLunchIn == null || dOut == null) {
event.value = "";
return;
}
var msMorning = dLunchOut.getTime() - dIn.getTime();
var msAfternoon = dOut.getTime() - dLunchIn.getTime();
// Guard against negative spans (AM/PM entered incorrectly)
if (msMorning < 0 || msAfternoon < 0) {
event.value = "";
return;
}
// ms → decimal hours (1000 × 60 × 60 = 3,600,000)
var totalHours = (msMorning + msAfternoon) / 3600000;
event.value = util.printf("%.2f", totalHours); // e.g. "8.50"
})();
A small note on testing: if you want to inspect what util.scand is returning, open All tools › Use JavaScript › Debugger (JavaScript Console) and run util.scand("h:MM tt", "08:00 AM") you should see a valid Date object.
I hope this helps.
Regards,
Anand Sri.