Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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

abbasm79416867
Level 2
Level 2

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)

8 Replies
_Bruce_Robertson
Level 10
Level 10

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

abbasm79416867
Level 2
Level 2

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

_Bruce_Robertson
Level 10
Level 10

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
Level 2
Level 2

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 = "";

    }

})();

abbasm79416867
Level 2
Level 2

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 = "";

    }

})();

_Bruce_Robertson
Level 10
Level 10

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

TehnologiaInformatiei
Level 2
Level 2

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.

_Bruce_Robertson
Level 10
Level 10

Hi,

Try changing the line;

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

to

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

Regards

Bruce