Skip to main content
Participating Frequently
February 19, 2023
Answered

Need Help: Number of Days between several Dates

  • February 19, 2023
  • 2 replies
  • 6950 views

Team, 

Need some from the brain trust...

 

Total Travel Days should = the sum of all Dates Departing and all Dates Arriving.
The Total Non-Travel Days should = the sum of all Dates Arriving and all Dates Departing.
I cannot seem to get a formula/script that will auto calculate the days. Ex. Destination #1, Date
Departing and Date Arriving is the same. The return would be the same.
The Total Travel Days should = 2
The Total Non-Travel Days should reflect the sum of the days in between the total travel days.
If you travel on Mon and Thurs, you have 2 travel days.
Tues and Wed would be non-travel days and = 2 days

This topic has been closed for replies.
Correct answer Thom Parker

Ok. I am not a Java guru. so where does that fit into this formula?

var dd = this.getField("D1Depart_af_date").value;
var rd = this.getField("D1Arrive_af_date").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;


No need to be a guru.  It's just a matter of looking over the different sequences of code shown in this thread, including yours, and noting the differences. 

Here's your code updated with data checking.  It's very similar to the original code I posted.

 

var nDiffDays = "";
var dd = this.getField("D1Depart_af_date").value;
var rd = this.getField("D1Arrive_af_date").value;
if(dd.length && rd.length)
{
    var d1 = util.scand("mm/dd/yy", dd);
    var d2 = util.scand("mm/dd/yy", rd);
    if(d1 && d2)
        nDiffDays = Math.floor((d2 - d1)/86400000) + 1;
}
event.value = nDiffDays;

 

 

 

2 replies

Techcon66Author
Participating Frequently
February 19, 2023

I've tried several scripts. But nothing happens. No error message, nothing. I can post the ones I've used if it helps.

Thom Parker
Community Expert
Community Expert
February 19, 2023

I would suggest doing this. Create a new test form that only calculates the number of days between two dates.

Read this first:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

You can read more about date processing here:

https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm

https://acrobatusers.com/tutorials/date_time_part1/

https://acrobatusers.com/tutorials/date_time_part2/

 

Also Read this:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

 

 

Here's some test code.   

// First, acquire the date strings 

var nDiffDays = -1;
var strDateStart = this.getField("StartDate").valueAsString;
var strDateEnd = this.getField("EndDate").valueAsString;

if(strDateStart.length && strDateEnd.length)
{ // convert the date strings to date objects
     var oDateStart = util.scand("ddd, mmm dd, yyyy", strDateStart);
     var oDateEnd = util.scand("ddd, mmm dd, yyyy", strDateEnd);

     if(oDateStart && oDateEnd)
     {// Get difference in milliseconds
           var nDiffMs = oDateEnd - oDateStart;
           // Calculate Days
          nDiffDays = Math.floor(nDiffMs/86400000);
     }
}

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Techcon66Author
Participating Frequently
February 19, 2023

Ok. Thanks. I'll try that. Here are the ones I've tried to use up to this point.

// Custom calculate script
(function () {

var sStart = getField("D1Arrive").valueAsString;
var sEnd = getField("ReturnDepart").valueAsString;
var dStart, dEnd, diff;

if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5);
} else {
event.value = "";
}

})();

var dd = this.getField("D1Arrive").value;
var rd = this.getField("ReturnDepart").value;

var d1 = util.scand("m/d/yy", dd);
var d2 = util.scand("m/d/yy", rd);

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

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

Thom Parker
Community Expert
Community Expert
February 19, 2023

There are no scripts on this file. What scripts have you tried. There are many posts on this forum that provide code for calculating the # of days between dates. 

 

 

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