Insert numbers of days in a field and calculate next date excluding Weekend days (Saturday, Sunday)

abbasm79416867 04-03-2016

Hi, I have three fields like this:

"Date field", "Numbers of Day"s and "Calculate Date fields"

I like to calculate like this: Date field + Numbers of Days = Calculate Date (Excluding Weekend days)

Mark Solution

Are these answers useful?
Help other community members by marking useful answers as accepted.

Accepted Solutions (0)

Answers (8)

Answers (8)

BR001
MVP
09-03-2016

Hi,

Try changing the line;

var calculateDate = addWorkingDays(d, DaysAdded10.rawValue);

to

var calculateDate = addWorkingDays(d, DaysAdded10.rawValue - 1);

Regards

Bruce

lucian_bota 09-03-2016

Hi Bruce,

How can you modify the script so that the calculation works as follows:

For example the date:

09.03.2016 + 1 day should return 09.03.2016

09.03.2016 + 3 days should return 11.03.2016

09.03.2016 + 5 days should return 15.03.2016

The script above is OK for me but always the result should be minus one day.

BR001
MVP
08-03-2016

Hi,

Are you using Adobe LiveCycle Designer or Acrobat to edit your form, that is it an XFA Form or a AcroForm.  This code would suggest you are working on an AcroForm, in which case you would be better off in a different forum, maybe this one PDF Forms

If you are using Designer then try this code in the calculate event of the calculate date field.

var Days = { Sunday : 0, Monday : 1, Tuesday : 2, Wednesday : 3, Thursday : 4, Friday : 5, Saturday : 6 };

function addWorkingDays(startDate, days)

{

    var weekend = [Days.Saturday, Days.Sunday];

    for (var currentDate = startDate; days > 0; currentDate.setDate(currentDate.getDate()+1))

    {

        if (weekend.indexOf(currentDate.getDay()) < 0)

        {

            days--;

        }

    }

    return currentDate;

}

if (!OriginalDate2.isNull && !DaysAdded10.isNull)

{

  var sDate = OriginalDate2.formattedValue;

  var d = util.scand("dddd d mmm yyyy", sDate);

  var calculateDate = addWorkingDays(d, DaysAdded10.rawValue);

  isNaN(calculateDate) ? null : util.printd("YYYY-MM-DD", calculateDate, true);

}

This document explains the differences http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/AcroJS_DesignerJS.pdf

Regards

Bruce

abbasm79416867 08-03-2016

This below is another code, but problem is that this code skip only first weekend days. For example when i add 14 days, it only skip one weekend days, whereas it should skip all weekend days. // Custom Calculate script for NewDate field

(function () {

    // Get date entered into the OrigianlDate field

    var sDate = getField("OriginalDate2").valueAsString;

    // Convert string to date

    var d = util.scand("dddd d mmm yyyy", sDate);

    // Get the number of days added, as a number

    var days = getField("DaysAdded10").value;

    // Add days to date

    d.setDate(d.getDate() + days);

   // possible adjustment for weekend

   var nAdj = 0;

    // get the zero based day of the week starting on Sunday

    var nDay = d.getDay();

    // test for Sunday or a value  day value of 0

     if(nDay == 0) nAdj = 1; // bump to Monday

    // test for Saturday or a day value of 6

    if(nDay == 6) nAdj = 2; // bump to Monday

   // adjust date

   d.setDate(d.getDate() + nAdj);   // adjust computed date by needed bump

    if (sDate) {

        event.value = util.printd("dddd d mmm yyyy", d);

    } else {

        event.value = "";

    }

})();

abbasm79416867 08-03-2016

The code is not working.

Please again see below script, it is working good, but only it not skip all Saturdays and Sunday:

// Custom Calculate script for NewDate field

(function () {

    // Get date entered into the OrigianlDate field

    var sDate = getField("OriginalDate2").valueAsString;

    // Convert string to date

    var d = util.scand("dddd d mmm yyyy", sDate);

    // Get the number of days added, as a number

    var days = getField("DaysAdded10").value;

    // Add days to date

    d.setDate(d.getDate() + days);

    // Populate this field with the result

    if (sDate) {

        event.value = util.printd("dddd d mmm yyyy", d);

    } else {

        event.value = "";

    }

})();

BR001
MVP
06-03-2016

Hi,

Try changing the line of code;

d.setDate(d.getDate() + days);

To

d = addWorkingDays(d, days)

(and add the rest of my code above at the top)

Bruce

abbasm79416867 06-03-2016

Hi, It's not working yet, I have three fillable fields named as:

"Date field"

"Days"

"Calculate Date"



1. I want that that i can enter date in "startDate" with this format: dd mmm yyyy

2. Then i enter numbers of days  in "days" fields

3. I want the result in "Calculate Date" field but i want that script count 5 days in a week. (Skip all weekend days like Saturday and Sunday)


Please see below as sample


Sunday 6 Mar 2016 + 20 days = Monday 4 Apr 2016  (Skip all weekend days)


Please, see below script, but there is problem that this script not skip weekend days:


// Custom Calculate script for NewDate field

(function () {

    // Get date entered into the OrigianlDate field

    var sDate = getField("OriginalDate2").valueAsString;

    // Convert string to date

    var d = util.scand("dddd d mmm yyyy", sDate);

    // Get the number of days added, as a number

    var days = getField("DaysAdded10").value;

    // Add days to date

    d.setDate(d.getDate() + days);

    // Populate this field with the result

    if (sDate) {

        event.value = util.printd("dddd d mmm yyyy", d);

    } else {

        event.value = "";

    }

})();

Please amend this code, so that week should count 5 days in a week or skill all Saturday and Sunday

BR001
MVP
04-03-2016

Hi,

Try this JavaScript function,

var Days = { Sunday : 0, Monday : 1, Tuesday : 2, Wednesday : 3, Thursday : 4, Friday : 5, Saturday : 6 }; 

function addWorkingDays(startDate, days)

{

    var weekend = [Days.Saturday, Days.Sunday];

    for (var currentDate = startDate; days > 0; currentDate.setDate(currentDate.getDate()+1))

    {

        if (weekend.indexOf(currentDate.getDay()) < 0)

        {

            days--;

        }

    }

    return currentDate;

}

 

You need to pass in the startDate (as a JavaScript object) and the number of days.

You probably want to add it to the calculate event of the "Calculate Date" field.

Regards

Bruce