Skip to main content
Inspiring
April 21, 2018
Answered

Calculation problem: Need script modification

  • April 21, 2018
  • 2 replies
  • 2403 views

Hi

I am not good at JavaScript coding. I searched through stackoverflow and adobe forum to find a solution for my leave form for my shop. Finally I managed to come up with below code.

Point 1: Thursday and Friday will be excluded as weekend. It is done.

Point 2: Public holiday should be excluded. If public holiday falls in a working day (Saturday-Wednesday) it is excluding from calculation.

Problem: If public holiday falls in weekend (Thursday-Friday), it is deducting for both (holiday & weekend). Suppose leave duration is 18/09/2018-22/09/2018, total count 2 days is showing in place of 3. Again for 17/10/2018-21/10/2018,total count 1 day is showing in place of 3 days

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

// get the start date value

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

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

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

event.value = dateDifference(start, end);

function dateDifference(start, end) {

  // Copy date objects so don't modify originals

  var s = new Date(+start);

  var e = new Date(+end);

  // Set time to midday to avoid daylight saving and browser quirks

  s.setHours(12,0,0,0);

  e.setHours(12,0,0,0);

  // 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 Sunday or Saturday, add to business days

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

        ++days;

      }

    }

  }

var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/21","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];

//test for public holidays

var phdays = 0;

for (var i = 0; i <hdayar.length; i++){

if ((Date.parse(hdayar) >= Date.parse(start)) && (Date.parse(hdayar) <= Date.parse(end))) {phdays ++;}}

  return days-phdays + 1;

}

There might be a small modification in my code will do the trick. But I am not getting it.

Any help or any idea to solve the problem would be great!

Regards

This topic has been closed for replies.
Correct answer jahantech

Finally I came up with the code that was needed with the help of google.

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

// get the start date value

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

// get the end date value

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

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

event.value = dateDifference(start, end);

function dateDifference(start, end) {

if (end < start) return "";

  // Copy date objects so don't modify originals

  var s = new Date(+start);

  var e = new Date(+end);

  // Set time to midday to avoid daylight saving and browser quirks

  s.setHours(12,0,0,0);

  e.setHours(12,0,0,0);

  // 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() != 4 && s.getDay() != 5) {

        ++days;

      }

    }

  }

var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/2 1","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];

//test for public holidays

var phdays = 0;

var weekend = [4, 5],   // for Thursday, Friday

    holDate, holDay;

for (var i = 0; i < hdayar.length; i++){

    holDate = Date.parse(hdayar);

    holDay = new Date(holDate).getDay()

    if (weekend.indexOf(holDay) == -1 && holDate >= Date.parse(start) && holDate <= Date.parse(end)) {

        phdays ++;

    }

}

return days-phdays + 1;

}

If anyone improves the code requested to upload so that other can be get benefited as it is an user to user forum and most of the people are helping each other here without money.

Regards

2 replies

Known Participant
April 24, 2018

how did you tried it and it worked since it is not working for me.

could you send me the pdf so i can copy the correct code and use it in my forms

my form has

Date1 | Date2 | Total Days

but the Date2 is the joining date which means that the total days should show all days except the joining day and the weekends as i already posted a new question

PDF Form Days between two Dates Excluding Weekends

jahantechAuthor
Inspiring
April 24, 2018

Use 5 and 6 instead of 4 & 5 in var weekend and

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

Known Participant
April 24, 2018

Dear jahantech! thank you for your reply, my code is now as 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

   

  }

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

check it well and please i have more questions as mentioned above.... will repeat again

1. Fridays and Saturdays are weekends (need to be excluded)

2. The Total Days field should be blank till the user fills both date1 and date2 or it should be blank

jahantechAuthorCorrect answer
Inspiring
April 21, 2018

Finally I came up with the code that was needed with the help of google.

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

// get the start date value

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

// get the end date value

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

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

event.value = dateDifference(start, end);

function dateDifference(start, end) {

if (end < start) return "";

  // Copy date objects so don't modify originals

  var s = new Date(+start);

  var e = new Date(+end);

  // Set time to midday to avoid daylight saving and browser quirks

  s.setHours(12,0,0,0);

  e.setHours(12,0,0,0);

  // 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() != 4 && s.getDay() != 5) {

        ++days;

      }

    }

  }

var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/2 1","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];

//test for public holidays

var phdays = 0;

var weekend = [4, 5],   // for Thursday, Friday

    holDate, holDay;

for (var i = 0; i < hdayar.length; i++){

    holDate = Date.parse(hdayar);

    holDay = new Date(holDate).getDay()

    if (weekend.indexOf(holDay) == -1 && holDate >= Date.parse(start) && holDate <= Date.parse(end)) {

        phdays ++;

    }

}

return days-phdays + 1;

}

If anyone improves the code requested to upload so that other can be get benefited as it is an user to user forum and most of the people are helping each other here without money.

Regards