Skip to main content
July 29, 2017
Question

Excluding weekends and holidays

  • July 29, 2017
  • 2 replies
  • 1529 views

Hello,

May be the question has already been answered but I am strugling to find the right answer for my situation.

I have a pdf form. The user input a date "from" and a date "to". My script is able to get the difference in days. But how:

  1. exclude weekends
  2. create a fuction with all holidays in a year to automatically exclude them during calculation?

Here is my script. Please take into consideration my input field, I am new to adobe script.

// get the end date value

var cEnd = this.getField("To").value;

// get the start date value

var cStart = this.getField("From").value;

event.value = "";

// compute number of days

if(cEnd != "" & cStart != '') {

   // convert date strings to objects

   var oEnd = util.scand("dd/mmm/yyyy H:MM:SS", cEnd + " 0:00:00");

   var oStart =util.scand("dd/mmm/yyyy H:MM:SS", cStart + " 0:00:00");

   // convert into days since epoch date

   var nEnd = Math.floor(Number(oEnd) / (1000 * 60 * 60 * 24));

   var nStart = Math.floor(Number(oStart) / (1000 * 60 * 60 * 24));

   // compute difference

   event.value = nEnd - nStart;

}

This topic has been closed for replies.

2 replies

July 30, 2017

Got it.

// get the end date value

var Date1 = this.getField("From").value;

// get the start date value

var Date2 = this.getField("To").value;

var dDate2 = util.scand("dd/mmm/yyyy H:MM:SS", Date2 + " 0:00:00");

var dDate1 =util.scand("dd/mmm/yyyy H:MM:SS", Date1 + " 0:00:00");

event.value = calcBusinessDays(dDate1, dDate2);

function calcBusinessDays(dDate1, dDate2) { // input given as Date objects

    var iWeeks, iDateDiff, iAdjust = 0;

    if (dDate2 < dDate1) return -1; // error code if dates transposed

    var iWeekday1 = dDate1.getDay(); // day of week

    var iWeekday2 = dDate2.getDay();

    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7

    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;

    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend

    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays

    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)

    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

    if (iWeekday1 <= iWeekday2) {

      iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)

    } else {

      iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)

    }

    iDateDiff -= iAdjust // take into account both days on weekend

    return (iDateDiff + 1); // add 1 because dates are inclusive

  }

JR Boulay
Community Expert
Community Expert
July 30, 2017
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
July 29, 2017

You will need to study the various methods of the JavaScript Date object. For example, getDay can be used to skip weekends.

Skipping holidays is more complicated. First of all, this information is not available in JS. You will need to define a list of such days (including the year) and then have your script compare them to the dates you're processing.

If you're interested, I've already developed a script that does all of that.

You can get it here: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation