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

Calculating number of days in a PDF form

New Here ,
Nov 06, 2019 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);

TOPICS
Acrobat SDK and JavaScript

Views

1.9K

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 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. 

 

 

Votes

Translate

Translate
Community Expert , 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
...

Votes

Translate

Translate
Community Expert ,
Nov 06, 2019 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
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 12, 2019 Nov 12, 2019

Copy link to clipboard

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 😞

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

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 12, 2019 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:

 DatesExample2_blank.pdf

  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 

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 11, 2019 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 = "";

 

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 12, 2019 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;

 

 

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 13, 2019 Nov 13, 2019

Copy link to clipboard

Copied

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

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 ,
Jul 05, 2023 Jul 05, 2023

Copy link to clipboard

Copied

Thank you for this formula, It helped me a lot with the form that I am working on.

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 ,
Jul 05, 2023 Jul 05, 2023

Copy link to clipboard

Copied

LATEST

You're welcome!

 

Happy to read that it helped.

 

All kudos to @Thom Parker and all of his well-documented tutorials.

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