Weird error when calculating difference between dates

New Here ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

Hi, I have seen posts on how to calculate the difference between two dates, but don't see mention of the error I am seeing.

 

Code in total field's custom calculation:

Custom calculate script

(function () {

 

var sStart = getField("jailIn1").valueAsString;

var sEnd = getField("jailOut1").valueAsString;

var dStart, dEnd, diff;

 

if(sStart && sEnd) {

dStart = util.scand("m/d/yyyy", sStart);

dEnd = util.scand("m/d/yyyy", sEnd);

 

//Below two lines are miscalc-ing for March 1-9, 2020 as start dates - drops a day if total is more than 9

diff = dEnd.getTime() - dStart.getTime();

event.value = Math.floor(diff / 864e5)+1;

 

} else {

event.value = "";

}

 

})();

 

(Editor made changes, can't tell if it broke the code)

 

This calculates the number of days between my in and out dates. (If they are the same date, the total should be 1.) 

 

It works fine in Edge, but not Acrobat.  In Acrobat it works EXCEPT if the in date is March 1-9, 2020 AND the total days is supposed to be 9 or more. In that narrow case, the total days low by 1.  See attached example.

 

I can't figure out why Acrobat is having an issue with these specific dates.  I tried adjusting the date formats, but the error persists.  Any suggestions welcome.

TOPICS
General troubleshooting , How to , JavaScript , PDF forms

Views

182

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 ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

Where's the dateDiff function?

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 ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

The code you posted is not what's used in the file...

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
New Here ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Sorry, I can't access this site from work, so had to email the code to my phone - the site editor made changes when I tried to post.  Thanks to my dyslexia, there is no way I can accurately proof code between my phone and laptop.

 

The code is the typical code posted to sites like this - please refer to the attached file.  The code works just fine - except for this narrow range of start dates.

 

I tried switching to dateDiff, but that threw an error, saying dateDiff doesn't exist.  That function is included with Adobe Sign, but doesn't seem to work with plain ol' Acrobat Pro.

 

I'm considering adding code to look for the problem dates, and skipping the calculation if one of the problem dates is entered.... but I have hundreds of fields to update, so I want to know if there's a better solution before doing all that work.

 

I don't know if any other dates have this problem.  I checked 2019-2022, which is the typical date range my users would enter.

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Adobe Sign does not support scripting at all. There's no built-in function named dateDiff in it, or in Acrobat, which is why your code doesn't work.

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
New Here ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Sigh... the attached file had both fields I tried to get to work, and fields with the original code.  The first field included two extra lines of code - trying to get dateDiff to work.  All of the rest of the fields just have the original code.

 

Let's try this again (Attached is a copy with the dateDiff code commented out.).  The code at issue is:

// Custom calculate script
(function () {

var sStart = getField("jailIn2").valueAsString;
var sEnd = getField("jailOut2").valueAsString;
var dStart, dEnd, diff;

if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}

})();

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

Works fine for me. I clear the second field, then re-enter "03/9/2020" into it and the result is 9.

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
New Here ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

I said it works in Edge, but not in Acrobat Pro or Adobe Reader (latest versions).

What software/version are you using?

 

This is what I see when entering 3/1/2020 and 3/9/2020... 8 days instead of 9.

sample.png

 

 

 

 

 

Any ideas why it is not working for multiple people on my end, but is on yours?

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 ,
Apr 14, 2022 Apr 14, 2022

Copy link to clipboard

Copied

I used both Reader DC and Acrobat XI Pro, and got the same results in both. I can try it in Acrobat DC Pro later on.

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 ,
Apr 15, 2022 Apr 15, 2022

Copy link to clipboard

Copied

I get 9 days in Adobe Acrobat DC.

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
New Here ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Thanks so much for testing this out.

 

I went back and found more staff to test this with - where I'm physically present so I can confirm the way this is working.

 

It turns out that the issue is in the the latest version of Acrobat Pro - yet only some users see this error.  I can't determine what is triggering the error, but it is consistent - the calculation either alway works, or always breaks. Yes, this calculation script is working differently for users with the exact same version of Pro.  Since I can't force staff to use Reader vs. Pro, I'll have to add something to the calculation to look for the problem dates, then let users enter the total manually.  (Or just remove the calculations entirely.)

 

Thanks again!

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

What does you mean with latest version?

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
New Here ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Acrobat Pro 22022.001.20085.

 

And someone just said they saw the problem in Reader 2022.001.20085 - though they have both Reader and Pro on their laptop.  I'm not seeing the issue with anyone that has this version of Reader only (no Pro installed).

 

So, can't use this code - can't trace what's causing the error.  I'll probably remove the code vs. trying to program around these dates.

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

One possible cause I can think of for this type of error is caused by the way you're calculating the difference between the two dates, ie, dividing the number of milliseconds by the number of ms per day. The issue with doing it like that is that number is not always the same. Namely, when there's a change to daytime savings an extra hour is added to that day (or reduced, when changed back) and it will throw off this type of calculation if you don't account for that. Now, the dates you mentioned do not correspond with the usual time when this happens, so I'm not sure that's the issue here, but it might be, depending on your location.

The way to account for it is to use Math.round, instead of Math.floor, as that will round the number either up or down to the nearest integer.

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Another option is to calculate the difference between the two dates using a loop, starting from the first one and adding one day until reaching the last one. That circumvents this issue entirely.

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

The actual version for Windows is 22.001.20117

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
New Here ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Okay, so my IT staff is wrong - the Adobe site says 22.001.2011x is the latest version. It will take me a few days to have that installed.

 

Thanks for the suggestion about programming a loop to do the calcuations, and that the current code doesn't account for time shifts.  Unfortunately, I am not a programmer.  I'll have to find an online class or tutorial to figure that out. (Which I will do - I just had no idea what direction to go in before.)

 

It might be a few  weeks, but I'll post again once I work out replacement code.

 

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

LATEST

Try this code:

 

 

var diff = 0;
while (util.printd("mm/dd/yyyy", dStart)!=util.printd("mm/dd/yyyy", dEnd)) {
	dStart.setDate(dStart.getDate()+1);
	diff++;
}
if (diff>0) diff++;

 

Edited: Fixed a small error in the code...

 

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