Skip to main content
Known Participant
April 23, 2018
Question

PDF Form Days between two Dates Excluding Weekends

  • April 23, 2018
  • 3 replies
  • 4020 views

Want to get total days from two dates

Date3

Date4

the total days should exclude weekends "Fridays" and "Saturdays"

I'm pushing the script in properties of the Total field.

could anybody check if I'm doing something wrong because the calculation is not showing the correct number.

the code is given below:

----------------------------------------------------------------------------------------------------

// get the end date value

var Date4 = this.getField("Date3").value;

// get the start date value

var Date3 = this.getField("Date4").value;

var dDate3 = util.scand("dd/mmm/yyyy", Date3);

var dDate4 =util.scand("dd/mmm/yyyy", Date4);

event.value = calcBusinessDays(dDate4, dDate3);

if(event.value == 0) event.value = "";

function calcBusinessDays(dDate4, dDate3) { // input given as Date objects

    var iWeeks, iDateDiff, iAdjust = 0;

    if (dDate3 < dDate4) return -1; // error code if dates transposed

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

    var iWeekday2 = dDate3.getDay();

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

    iWeekday2 = (iWeekday2 == 5) ? 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((dDate3.getTime() - dDate4.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

   

  }

----------------------

This topic has been closed for replies.

3 replies

Inspiring
April 26, 2018

Ask in LCD forum. Someone may help there

Inspiring
April 25, 2018

Delete everything from totaldays field and put below code

var start = this.getField("Date3").value; // get the start date value

var end = this.getField("Date4").value; // get the end date value

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

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

event.value = dateDifference(start, end);

function dateDifference(start, end) {

if(start != null && end != null) {

  // Copy date objects so don't modify originals

  var s = new Date(+start);

  var e = new Date(+end);

  // Get the difference in whole days

  var totalDays = Math.round((e - s) / 8.64e7);

  // Get the difference in whole weeks

  var wholeWeeks = totalDays / 7 | 0;

  // Estimate business days as number of whole weeks * 5

  var days = wholeWeeks * 5;

  // If not even number of weeks, calc remaining weekend days

  if (totalDays % 7) {

    s.setDate(s.getDate() + wholeWeeks * 7);

    while (s < e) {

      s.setDate(s.getDate() + 1);

      // If day isn't a Thursday or Friday, add to business days

      if (s.getDay() != 5 && s.getDay() != 6) {

        ++days;

      }

    }

  }

return days + 1;

}

else ""

}

Known Participant
April 26, 2018

jahantech, thank you for help and prompt replies.

i already mentioned that now I'm using Adobe LiveCycle Designer so how i can use your code? i can use it in Acrobat but since now im creating the form in LC so how to do it?

Known Participant
April 24, 2018

till now i have made some changes in my script,

its just excluding Fridays and Saturdays as i requested.

you can check in the following updated script, but now what i want is the Total Days field should be blank till the user fills the both dates fields what should i do?

the updated script is given below:

// get the end date value

var Date4 = this.getField("Date3").value;

// get the start date value

var Date3 = this.getField("Date4").value;

var dDate3 = util.scand("dd/mmm/yyyy", Date3);

var dDate4 =util.scand("dd/mmm/yyyy", Date4);

event.value = calcBusinessDays(dDate4, dDate3)-1;

if(event.value == 0) event.value = "";

function calcBusinessDays(dDate4, dDate3) { // input given as Date objects

    var iWeeks, iDateDiff, iAdjust = 0;

    if (dDate3 < dDate4) return -1; // error code if dates transposed

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

    var iWeekday2 = dDate3.getDay();

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

    iWeekday2 = (iWeekday2 == 6) ? 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((dDate3.getTime() - dDate4.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

   

  }

Known Participant
April 24, 2018

can anyone help me please!

try67
Community Expert
Community Expert
April 24, 2018

It's really not that complicated... Just add an if-condition that if Date3 or Date4 are blank the result should be an empty string.