• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with time difference calculations: Negative value

New Here ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

I found a JavaScript for time difference calculations and am trying to understand why I am getting a negative value when entering a time into field 1 ("Start"). For instance, I enter 10:00PM into the "Start" field and have not entered a time into the second field "LunchIn", though I am getting a negative value calculated by the script (-22). The problem is corrected when time is entered into the second field, though I am wondering if there is a way to prevent the script from calculating the time difference when only one field has input. 

 

function sTime2Minutes(cFormat, sValue) {
// convert date time string number of minutes since epoch date;
// test for successful conversion
var oDate = util.scand(cFormat, sValue);
if(oDate == "") app.alert("Error in time conversion of " + sValue + " using format " + cForamt, 1, 0);
// return number of minutes;
return Number( util.printf("%,100.0f", oDate.getTime() / (1000 * 60) ) );
} // end sTime2Minutes function;

function Min2Time(nMinutes) {
// format number of minutes to hours ":" minutes;
return util.printf("%,001.0f:%,002.0f", Math.floor(nMinutes / 60), (nMinutes % 60));
} // end Min2Time function;

var cStart = "Start" // start field name;
var cEnd = "LunchIn"; // end field name;
// the time format string
var cTimeFormat = "h:mm tt";
// convert end time to minutes
var nEnd = sTime2Minutes("dd-mmm-yyyy " + cTimeFormat, "01-Jan-1970 " + this.getField(cEnd).valueAsString);
// get start time minutes
var nStart = sTime2Minutes("dd-mmm-yyyy " + cTimeFormat, "01-Jan-1970 " + this.getField(cStart).valueAsString);
// compute difference in minutes
var nDiff = nEnd - nStart;
// format the results for display;
// whole hours and remainder of minutes when divided by 60;
// event.value = Min2Time(nDiff);
// or
// format display to hours.decimal hours
event.value = util.printf("%,001.2f", nDiff / 60);

TOPICS
PDF forms

Views

868

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

correct answers 2 Correct answers

Community Expert , Nov 17, 2020 Nov 17, 2020

Change the field calculation order.

Votes

Translate

Translate
Community Expert , Nov 17, 2020 Nov 17, 2020

The problem with this form isn't the calculations themselves, it's the calculation order. The fields are completely out of sequence.

You'll fine the calculation ordering tool on the "More..." dropdown in the "Prepare Forms" panel

TEst.png

 

Votes

Translate

Translate
New Here ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

I am also getting a negative value when calculating a grand total with the basic sum function even though there are no negative totals on the sheet. 

Laura5CD6_0-1605305207740.png

 

Votes

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
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

How does you calculate the totals?

Votes

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
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

You'll need to share the entire file or post the code of all the internal functions used in your code (sTime2Minutes, Min2Time)...

Votes

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
New Here ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

Thank you. Any idea how to attach a PDF here? 

Votes

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
Community Expert ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

You can attach it to the original message using the tiny paperclip icon at the bottom when you edit it, or upload it to a file-sharing website (like Dropbox, Google Drive, Adobe Cloud, etc.), generate a share link and then post it here.

Votes

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
New Here ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

Thank you for your help. I wasn't able to edit the post.

Here is a link: https://drive.google.com/file/d/1eMm2HkF-EbroSXz-Lccet25nm33NFCK2/view?usp=sharing

Votes

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
Community Expert ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

Change the field calculation order.

Votes

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
Community Expert ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

The problem with this form isn't the calculations themselves, it's the calculation order. The fields are completely out of sequence.

You'll fine the calculation ordering tool on the "More..." dropdown in the "Prepare Forms" panel

TEst.png

 

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

Votes

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
New Here ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

Ah ha! I think that did the trick. Thanks a bunch 😄

Votes

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
New Here ,
Nov 29, 2020 Nov 29, 2020

Copy link to clipboard

Copied

Hello, I am getting negative numbers with this calculation.

I.e. start 22:45 End 03:15 result is -20:30 instead 04:30 I am trying to do flight time calculation departure time is 22:45 arrival time 03:15 next day, I need total hours flown

 

var hrsStart = parseInt(this.getField("time2_1").value.split(":")[0]); var minStart = parseInt(this.getField("time2_1").value.split(":")[1]); var hrsEnd = parseInt(this.getField("time4_1").value.split(":")[0]); var minEnd = parseInt(this.getField("time4_1").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; } this.getField("flight1").value = hrsRez + ":" + minRez; if(flight1 < 0) {// Shift 24 hours flight1 += 24; }

Votes

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
Community Expert ,
Nov 29, 2020 Nov 29, 2020

Copy link to clipboard

Copied

Check the Javascript console for errors.

Votes

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
Community Expert ,
Nov 30, 2020 Nov 30, 2020

Copy link to clipboard

Copied

First, never post code like this. Always format it so we can read it.

Now, as try67 pointed out, the last bit of code is definatley an error. There is no variable named "flight1". 

And this will cause an error that is reported in the console.

 

This code is a bit long winded, and to be "correct" it needs to include actual dates. With the dates you could simplify the calculation using the date object as the basis of the calc. 

 

However, since the assumtion is that flights are less than 24 hours and a starting time greater than the ending time represents a midnight crossing, this calculation is simpler and more compact.

 

// These functions only need to be defined once. Place them in a document script if 
// there are more fields that perform similar time calculations.
function TimeStr2Num(strTm)
{
    var aParts = strTm.value.split(":");
    return Number(aParts[0]) + aParts[1]/60;
}
 
function TimeNum2Str(nTm)
{
   var aParts = nTm.toString().split("."); 
   return util.printf("%02d:%02d",aParts[0],Number("."+aParts[1])*60);
}

// This is the main calculation code.
nDiff = TimeStr2Num(this.getField("time4_1")) - TimeStr2Num(this.getField("time2_1"));
if(nDiff < 0)
   nDiff += 24;
strTime = TimeNum2Str(nDiff);

 

If the 24hour assumption is true for all calculations the 24 hour correction could be rolled into the "TimeNum2Str" function.  

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

Votes

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
New Here ,
Nov 30, 2020 Nov 30, 2020

Copy link to clipboard

Copied

My apologies for the ugly code I sent, I am new.

Can someone take a look at the PDF form, it will make more sense.

if you see flight sector 1 Out - In equals 4:10 and Off - On equals -20:10 ( needs to show 4:10 )

Regards

https://www.dropbox.com/s/omenjl247x4xvkc/NXAL%20Aircraft%20Demo_Ferry%20Record.pdf?dl=0 

 

Votes

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
Community Expert ,
Dec 01, 2020 Dec 01, 2020

Copy link to clipboard

Copied

LATEST

Again, the code is a mess. I would suggest cleaning it up. I would also suggest using the time functions  I provide in my last post. 

 

If you would simply like someone to fix the form, I would be happy to do this for a fee. Please contact me through this forum. 

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

Votes

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