Please help with military time calculation

New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

I am trying to take 2 times (start time and end time) and have the total time determined.  I want it to come out so that if it is an hour and a half total it will look like 1.30 instead of 1.5.  I am using Acrobat Pro DC and it always calculates correctly but when I take the file and try it on a work computer which is a different version (not sure which) it comes up with a different answer. Could someone look and see what I am doing wrong please.  Sometimes the start time may be 23:00 and end at 00:30 so that is why I added the last part of the code but not what is wrong.  Thank You for any help!

 

var hrsStart = parseInt (this.getField("ST").value.split(":")[0]);
var minStart = parseInt (this.getField("ST").value.split(":")[1]);
var hrsEnd = parseInt (this.getField("ET").value.split(":")[0]);
var minEnd = parseInt (this.getField("ET").value.split(":")[1]);
if (minStart > minEnd) {
var minRez = 60 + minEnd - minStart;
var hrsRez = hrsEnd - 1 - hrsStart;
} else {
var minRez = minEnd - minStart;
var hrsRez = hrsEnd - hrsStart;
}
if (hrsRez < 0){
var hrsRez = hrsRez + 24;
event.value = hrsRez + "." + minRez;
} else {
var hrsRez = hrsRez + 0;
event.value = hrsRez + "." + minRez;}

 

TOPICS
Acrobat SDK and JavaScript, General troubleshooting, PDF forms

Views

282

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

A better strategy for your particular setup, is to convert the time values to hours. You can do the midnight crosss correction on either end of the calculation. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2019 Dec 06, 2019

Copy link to clipboard

Copied

LATEST

How do you deal with the the DST or Summer time changes?

 

One can multiply the decimal hours by 60 minutes to obtain the number of minutes.

 

If one includes the start and end dates it is possible to convert the date time strings into the JavaScript date object and then perform the calculations using the various methods and properties of the JavaScript date object.

 

I would convert the times to minutes since midnight and then format the result to include leading zeros for the minutes.

 

// get start hours and minutes;
var hrsStart = parseInt (this.getField("ST").value.split(":")[0]);
var minStart = parseInt (this.getField("ST").value.split(":")[1]);
// start time as minutes since midnight;
var timeStart = (hrsStart * 60) + minStart;
// get end hours and minutes;
var hrsEnd = parseInt (this.getField("ET").value.split(":")[0]);
var minEnd = parseInt (this.getField("ET").value.split(":")[1]);
// end time as minutes since midnight;
var timeEnd = (hrsEnd * 60) + minEnd;
// compute difference in minutes;
var timeRez = timeEnd - timeStart;
// assume adjust for next day in minutes;
if(timeRez < 0) {
timeRez += (60 * 24);
}
// display in result in formatted hours and formatted minutes including leading zeros;
event.value = util.printf("%,101d", Math.floor(timeRez / 60)) + ":" + util.printf("%,102d", (timeRez % 60));

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines