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);
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.
Use the Acrobat JavaScript Reference early and often
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;
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.
Use the Acrobat JavaScript Reference early and often
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 😞
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
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
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 = "";
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;
Copy link to clipboard
Copied
This worked perfectly! Thank you sooooo much - I have spent hours on this!
Copy link to clipboard
Copied
Thank you for this formula, It helped me a lot with the form that I am working on.
Copy link to clipboard
Copied
You're welcome!
Happy to read that it helped.
All kudos to @Thom Parker and all of his well-documented tutorials.
Copy link to clipboard
Copied
hello please help me with this code. I want the end of value description if the leave is 1 day only, the end of the value will be "day" but if the leave is more than 1 day the word after the value will be "days" Thank you so much
var strStart = this.getField("startdate").value;
var strEnd = this.getField("enddate").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))+ (" days");
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");
else if(strEnd=="" && strStart=="") event.value = "";
else if(strEnd==srtStart) event.value = "";
else event.value = "";
Copy link to clipboard
Copied
Change this line:
var days = Math.round(Math.ceil(diff/oneDay))+ (" days");
To:
var numDays = Math.round(Math.ceil(diff/oneDay));
var days = "" + numDays + " " + (numDays==1 ? "day" : "days");
Copy link to clipboard
Copied
[MOVED TO THE ACROBAT DISCUSSIONS]

