# Calculating number of days in a PDF form

New Here ,
Nov 06, 2019 Nov 06, 2019

Copied

I am trying to get the total number of days, including the Begin and the End date.  I keep coming up one day short.  For example, my Begin date is 8/1/19 and the End date is 8/3/19.  This should be 3 days, but my formula is calculating it as 2.

This is the script I'm using.  Can anybody help?

var dd = this.getField("Begin").value;

var rd = this.getField("End").value;

var d1 = util.scand("mm/dd/yy", dd);

var d2 = util.scand("mm/dd/yy", rd);

var diff = (d2.valueOf() - d1.valueOf()) / 1000;

event.value = Math.round((diff / 60 / 60) / 24);

TOPICS
Acrobat SDK and JavaScript

Views

112

Likes

Report

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

Adobe Community Professional , Nov 06, 2019 Nov 06, 2019
You have to add a day. The difference of two numbers in not inclusive. For example 4-3 = 1.  But the inclusive difference is 2.

Likes

Adobe Community Professional , Nov 12, 2019 Nov 12, 2019
So I did a little practice with what you have and here are two ways that worked on my end to bypass the inclusive difference that Thom mentioned: The first is using Thom Parker's script from his tutorial - again, it seems that when put together the Math.round and Math.field in this variable --->> var days = Math.round(Math.ceil(diff/oneDay));  get the results that you are looking for. I've changed the field names to what you have in your original script, so if you wan't to copy it and try it, pl...

Likes

7 Replies 7
Nov 06, 2019 Nov 06, 2019

Copied

You have to add a day. The difference of two numbers in not inclusive. For example 4-3 = 1.  But the inclusive difference is 2.

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 12, 2019 Nov 12, 2019

Copied

Where do I need to add the day?  I've tried about everything I can think of, but still coming up with a calculation of 2.  Sorry, I'm not very well-versed in Java 😞

Likes

Report

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

Copied

Hi,

Check out Thom Parker's tutorial Working with date and time in Acrobat JavaScript and review the steps you are trying to folow for each line of code that you're using:

PART 1:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

PART 2:

https://acrobatusers.com/tutorials/date_time_part2

PART 3:

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

Likes

Report

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

Copied

The javascript that you should be workign with is found in PART 2 of his tutorial and there is a test PDF file with examples that you can practice on:

Look at Number of Days field in that form and that is the script you need to work around with.

NOTE: In my first reply I didn't remember where did I used this code from; so now I am able to confirm it was DatesExample2  PDF test file

Likes

Report

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

Copied

Hi,

This below is a similar script that is working for me with a few additional workarounds.

The script is not my original work, probably Thom Parker can tell if it belongs to him, from one of his posts or Try67 whom  I follow them a lot. I took ideas from different posts in relation to calculating difference in days between two different dates.

The script also calculates the difference in time between the given dates in those fields so it seems to also be little more precise.

In my case , instead of using  diff  by itself, like in your example

--->>>>  event.value = Math.round((diff / 60 / 60) / 24);

I incorporated this variable with Math.round and Math.ceil:

var days = Math.round(Math.ceil(diff/oneDay))+ (" day(s):");

Also, at the end of the script I had to figure out how to set a few conditions so when the users of the form start messing around with leaving one date field blank but not the other I would still get the results that I need.

Again, I am also a beginner, and this is what is working for me, so far flawlessly, but I ask if this script could be improved with a more cleaner javascript writing, and without using folder level scripts functions

var strStart = this.getField("FIRSTDATE").value;
var strEnd = this.getField("LASTDATE").value;

if(strStart.length && strEnd.length)
{
var dateStart = util.scand("ddd, dd mmm yyyy",strStart);
var dateEnd = util.scand("ddd, dd mmm yyyy",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.round(Math.ceil(diff/oneDay))+ (" day(s):");
event.value = days;
if (dateEnd<dateStart) event.value = "";
if (dateEnd<dateStart) app.alert("PLEASE NOTE: The last date is a future date and it cannot be earlier than the first date!",1);
}

else if(strStart>strEnd && strEnd=="") event.value = (" 1 day(s):");
else if(strEnd=="" && strStart=="") event.value = "";
else if(strEnd==srtStart) event.value = "";
else event.value = "";

Likes

Report

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

Copied

So I did a little practice with what you have and here are two ways that worked on my end to bypass the inclusive difference that Thom mentioned:

• The first is using Thom Parker's script from his tutorial - again, it seems that when put together the Math.round and Math.field in this variable --->> var days = Math.round(Math.ceil(diff/oneDay));  get the results that you are looking for. I've changed the field names to what you have in your original script, so if you wan't to copy it and try it, please go ahead.

var dd = this.getField("Begin").value;
var rd = this.getField("End").value;
if(dd.length && rd.length)
{
var d1 = util.scand("mm/dd/yy",dd);
var d2 = util.scand("mm/dd/yy",rd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.round(Math.ceil(diff/oneDay));
event.value = days;
}

• The second work around is adding by 1 in the the event value like Thom suggested to you----------->>>  event.value = Math.round((diff / 60 / 60) / 24)+1;        I am using your script in this example which you can also try it out and see for yourself:

var dd = this.getField("Begin").value;

var rd = this.getField("End").value;

var d1 = util.scand("mm/dd/yy", dd);

var d2 = util.scand("mm/dd/yy", rd);

var diff = (d2.valueOf() - d1.valueOf()) / 1000;

event.value = Math.round((diff / 60 / 60) / 24)+1;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 13, 2019 Nov 13, 2019