Highlighted

Calculation problem: Need script modification

Participant ,
Apr 21, 2018

Copy link to clipboard

Copied

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

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

TOPICS
PDF forms

Views

577

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Calculation problem: Need script modification

Participant ,
Apr 21, 2018

Copy link to clipboard

Copied

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

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

TOPICS
PDF forms

Views

578

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 21, 2018 0
Participant ,
Apr 21, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2018 0
New Here ,
Apr 24, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2018 0
Participant ,
Apr 24, 2018

Copy link to clipboard

Copied

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

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2018 0
New Here ,
Apr 24, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2018 0
Participant ,
Apr 24, 2018

Copy link to clipboard

Copied

What is dt1

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2018 0
New Here ,
Apr 24, 2018

Copy link to clipboard

Copied

sorry it is Date3 and Date4 not 1 and 2

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2018 0
New Here ,
Apr 25, 2018

Copy link to clipboard

Copied

jahantech! could you please help me?

now im using Adobe LiveCycle Desiner and created own new form from scratch. so how i will be able to calculate (Date3 & Date4 excluding weekends Fridays & Saturdays)?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2018 0
Participant ,
Apr 25, 2018

Copy link to clipboard

Copied

This is acroform forum not LCD forum. I shared a code here

PDF Form Days between two Dates Excluding Weekends.

Hope it will work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2018 0
New Here ,
Apr 25, 2018

Copy link to clipboard

Copied

dear jahantech, following is the code which is so far working in Acrobat form, but does not work in LCD why?

could anyone help me with this as i am just creating the form in LCD.

// get the end date value

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

// get the start date value

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

var dDate1 = util.scand("dd/mmm/yyyy", Date1);

var dDate2 =util.scand("dd/mmm/yyyy", Date2);

event.value = calcBusinessDays(dDate2, dDate1)-1;

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

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

    var iWeeks, iDateDiff, iAdjust = 0;

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

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

    var iWeekday2 = dDate1.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((dDate1.getTime() - dDate2.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

    if (Date1=="" || Date2=="") event.value = "";

else {

  // do the rest of the calculation

}

  }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2018 0
Participant ,
Apr 25, 2018

Copy link to clipboard

Copied

Pls try to understand whoever is replying here is not professional. You are asking help in acrofom initially then changed your form to xfa form and asking help in acroforum. At first decide which form u want to continue and ask in relevant forum.

And before asking help from others at first show them that you have done some work already. What you are posting here is already available in acrobat forum.

Hope you understand!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2018 0
Adobe Community Professional ,
Apr 25, 2018

Copy link to clipboard

Copied

Use the forum for LiveCycle Designer.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2018 0
New Here ,
Apr 26, 2018

Copy link to clipboard

Copied

Please check the LCD forum as I've posted the question there.

reference: Total Days between two Dates Excluding Weekends in LCD

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 26, 2018 0