Skip to main content
Participant
November 6, 2019
Answered

Calculating number of days in a PDF form

  • November 6, 2019
  • 4 replies
  • 4566 views

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

Correct answer ls_rbls

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;

 

 

4 replies

JR Boulay
Community Expert
Community Expert
February 21, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
ls_rbls
Community Expert
ls_rblsCommunity ExpertCorrect answer
Community Expert
November 13, 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, 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;

 

 

Participant
November 13, 2019

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

ls_rbls
Community Expert
Community Expert
November 12, 2019

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

 

Thom Parker
Community Expert
Community Expert
November 6, 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. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
November 12, 2019

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 😞

ls_rbls
Community Expert
Community Expert
November 12, 2019

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