• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit Search
• Need Help: Number of Days between several Dates

Need Help: Number of Days between several Dates

Community Beginner ,
Feb 19, 2023 Feb 19, 2023

Copied

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

TOPICS
How to , JavaScript

Views

2.9K

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 Expert , Feb 20, 2023 Feb 20, 2023

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 && d``````
...

13 Replies 13
Community Expert ,
Feb 19, 2023 Feb 19, 2023

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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 Beginner ,
Feb 19, 2023 Feb 19, 2023

Copied

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

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 Expert ,
Feb 19, 2023 Feb 19, 2023

Copied

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

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

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

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

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

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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 Beginner ,
Feb 19, 2023 Feb 19, 2023

Copied

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

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 Expert ,
Feb 19, 2023 Feb 19, 2023

Copied

You can see the similarity in all of the different calculation scripts.  So that's how it's done!!

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

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 Beginner ,
Feb 20, 2023 Feb 20, 2023

Copied

Thom,

I have four "cheater fields" set up. Still having some issues with getting the travel days columns to show zero if no dates are selected. Had to add a +1 to the formula to ensure that the same day is counted as the travel day.

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;

I used the cheater fields so I can sum the totals. I'm sure there is better way. I am not a Java guru, as you can tell.

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 Expert ,
Feb 20, 2023 Feb 20, 2023

Copied

If no dates are selected, then the date fields are empty, correct?

Did you see the part of the script that I posted, that uses an "if" statement to test for empty date strings?

Similar code is also in the scripts you posted.

That's the part you need.

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

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 Beginner ,
Feb 20, 2023 Feb 20, 2023

Copied

Thom,

Heres the challenge. If I do not leave the +1 in the formula, I get a zero when I put in the depart date and arrive date as the same. That is one travel day. It will reflect 0.

However if I take the +1 out of the formula, the total travel days is 0.

If I then put in the dates, don't alter the formula, it will reflect 0.

if travel is on Monday and Friday, that is two travel days. Tu - Thr are 3 non-travel days.

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 Beginner ,
Feb 20, 2023 Feb 20, 2023

Copied

Even if I remove the dates it still shows the total as 1. as the attachment shows. But If I take the +1 out of the formula then it will not recognize the first day as 1 day of travel.

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 Expert ,
Feb 20, 2023 Feb 20, 2023

Copied

None of those issues are important.

The important bit, is to detect when the date input is invalid (i.e. Empty), and not perform the calculation in that case.

For example, this code:

``````... Get Date Strings ...
if(dd.length && rd.length)
{
... perform calculation ...
}
else
event.value = 0;

``````

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

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 Beginner ,
Feb 20, 2023 Feb 20, 2023

Copied

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;

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 Expert ,
Feb 20, 2023 Feb 20, 2023

Copied

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

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

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 Beginner ,
Feb 21, 2023 Feb 21, 2023

Copied

LATEST

Thom,

Thank you again. Good to Go now. Appreciate all the help.