Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Home
- >
- Acrobat SDK
- >
- Calculating number of days in a PDF form

New Here
,

Nov 06, 2019

Copy link to clipboard

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);

Adobe Community Professional

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;**

TOPICS

Acrobat SDK and JavaScript

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 06, 2019

Copy link to clipboard

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);

Adobe Community Professional

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;**

TOPICS

Acrobat SDK and JavaScript

Community Guidelines

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

BethTodd

Nov 06, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/td-p/10719851
7
Replies
7

Adobe Community Professional
,

Nov 06, 2019

Copy link to clipboard

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.com- Use the Acrobat JavaScript Reference Early and Often

Community Guidelines

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

Reply

Loading...

Thom Parker

Nov 06, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10719868#M9306
New Here
,

Nov 12, 2019

Copy link to clipboard

Copied

Community Guidelines

Reply

Loading...

BethTodd

Nov 12, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10734531#M9308
Adobe Community Professional
,

Nov 12, 2019

Copy link to clipboard

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

Community Guidelines

Reply

Loading...

ls_rbls

Nov 12, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10734705#M9309
Adobe Community Professional
,

Nov 12, 2019

Copy link to clipboard

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

Community Guidelines

Reply

Loading...

ls_rbls

Nov 12, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10734718#M9310
Adobe Community Professional
,

Nov 11, 2019

Copy link to clipboard

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 = "";

Community Guidelines

Reply

Loading...

ls_rbls

Nov 11, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10731994#M9307
Adobe Community Professional
,

Nov 12, 2019

Copy link to clipboard

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;**

Community Guidelines

Reply

Loading...

ls_rbls

Nov 12, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10734987#M9311
BethTodd
LATEST

New Here
,

Nov 13, 2019

Copy link to clipboard

Copied

This worked perfectly! Thank you sooooo much - I have spent hours on this!

Community Guidelines

Reply

Loading...

BethTodd

Nov 13, 2019
0
/t5/acrobat-sdk/calculating-number-of-days-in-a-pdf-form/m-p/10737300#M9312
Using the Community
Experience League
Terms of Use
Privacy Policy
Cookie preferences
AdChoices
Language:

- Deutsch
- English
- Español
- Français
- 日本語コミュニティ
- Português

Copyright © 2020 Adobe. All rights reserved.