Skip to main content
Inspiring
March 24, 2019
Answered

Calculate Date without holidays and weekends

  • March 24, 2019
  • 2 replies
  • 6070 views

Hello,

i have the following script to calculate a user entered amount of days to my date. but now i want to integrate that i can specify custom holidays for my region an weekends, so that the user only have to enter workdays.

thats my script now:

  1. var originalDateString = this.getField("DatumTest").valueAsString; 
  2. var daysString = this.getField("Arbeitstage").valueAsString; 
  3. if (originalDateString=="" || daysString=="") event.value = ""
  4. else
  5.     var d = util.scand("dd/mm/yyyy", originalDateString); 
  6.     var days = Number(daysString); 
  7.     d.setDate(d.getDate()+days); 
  8.     event.value = util.printd("dd.mm.yyyy", d); 
  9. }

i think i need a counter who count weekends and my days specified and then calculte the above date - , but i have no idea how to begin...

i use adobe acrobat standard DC

This topic has been closed for replies.
Correct answer emddummi

it works. now i have the code with help from you two buddies. thanks for your patience.

i paste it here, because so i can probalbly help other people.

function returnfinaldate() {

var varHolidays = [

        new Date(2019, 03, 19),        // good friday

        new Date(2019, 03, 22),        // easter monday

        new Date(2019, 04, 30),        // ascension day

        new Date(2019, 05, 10),        // whit monday

        new Date(2020, 03, 10),        // good friday

        new Date(2020, 03, 13),        // easter monday

        new Date(2020, 04, 21),        // ascension day

        new Date(2020, 05, 01)        // whit monday   

    ];

    // define fix holidays here, you don't need to change the year

    var fixHolidays = [

        new Date(2019, 00, 01),

        new Date(2019, 02, 08),    // womens day in Berlin

        new Date(2019, 04, 01),

        new Date(2019, 09, 03),

        new Date(2019, 11, 24),

        new Date(2019, 11, 25),

        new Date(2019, 11, 26),

        new Date(2019, 11, 31)

    ];

  var startDate = util.scand("dd/mm/yyyy", this.getField('EingangC2').value);

  var endDate = util.scand("dd/mm/yyyy", new Date());

  var noOfDaysToAdd = this.getField('ATbisFertig').value;

  var count = 0;

  endDate = startDate;

        while (count < noOfDaysToAdd) {

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

            if (endDate.getDay() == 0 || endDate.getDay() == 6) {

count--;

}

var isHoliday = false

           // check variable holidays

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

               if (    (endDate.getDate() == varHolidays.getDate())

                       && (endDate.getMonth() == varHolidays.getMonth())

                       && (endDate.getFullYear() == varHolidays.getFullYear()))

               {

                   isHoliday = true;

                   break;

               }

           }

  

   // check fix holidays

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

           if ((endDate.getDate() == fixHolidays.getDate()) && (endDate.getMonth() == fixHolidays.getMonth())) {

               isHoliday = true;

                   break;

           }

       }

if (isHoliday) {

           continue;

       }

       count++; 

   }

   return endDate; 

}

event.value = util.printd("dd.mm.yyyy", returnfinaldate());

2 replies

emddummiAuthorCorrect answer
Inspiring
April 1, 2019

it works. now i have the code with help from you two buddies. thanks for your patience.

i paste it here, because so i can probalbly help other people.

function returnfinaldate() {

var varHolidays = [

        new Date(2019, 03, 19),        // good friday

        new Date(2019, 03, 22),        // easter monday

        new Date(2019, 04, 30),        // ascension day

        new Date(2019, 05, 10),        // whit monday

        new Date(2020, 03, 10),        // good friday

        new Date(2020, 03, 13),        // easter monday

        new Date(2020, 04, 21),        // ascension day

        new Date(2020, 05, 01)        // whit monday   

    ];

    // define fix holidays here, you don't need to change the year

    var fixHolidays = [

        new Date(2019, 00, 01),

        new Date(2019, 02, 08),    // womens day in Berlin

        new Date(2019, 04, 01),

        new Date(2019, 09, 03),

        new Date(2019, 11, 24),

        new Date(2019, 11, 25),

        new Date(2019, 11, 26),

        new Date(2019, 11, 31)

    ];

  var startDate = util.scand("dd/mm/yyyy", this.getField('EingangC2').value);

  var endDate = util.scand("dd/mm/yyyy", new Date());

  var noOfDaysToAdd = this.getField('ATbisFertig').value;

  var count = 0;

  endDate = startDate;

        while (count < noOfDaysToAdd) {

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

            if (endDate.getDay() == 0 || endDate.getDay() == 6) {

count--;

}

var isHoliday = false

           // check variable holidays

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

               if (    (endDate.getDate() == varHolidays.getDate())

                       && (endDate.getMonth() == varHolidays.getMonth())

                       && (endDate.getFullYear() == varHolidays.getFullYear()))

               {

                   isHoliday = true;

                   break;

               }

           }

  

   // check fix holidays

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

           if ((endDate.getDate() == fixHolidays.getDate()) && (endDate.getMonth() == fixHolidays.getMonth())) {

               isHoliday = true;

                   break;

           }

       }

if (isHoliday) {

           continue;

       }

       count++; 

   }

   return endDate; 

}

event.value = util.printd("dd.mm.yyyy", returnfinaldate());

emddummiAuthor
Inspiring
April 10, 2019

I now have a value in the field. How do I make it possible that in retrospect, a manual date can be assigned? When I choose a new one, he jumps back to the calculated date.

Inspiring
March 24, 2019

You should be able to compute the number of weekend days by the number of days being added and the start and the days of the week of the start and end dates. The ".getDay()" method will provide you with the day of the week for a given date as a zero based value starting on Sunday. Sunday will be zero and Saturday will be 6.

What are the valid days of the week for the "DatumTest" date?

Starting on a weekend day makes the calculation more complex.

As for the holidays, you will need to enter the holidays into the script and see if any fall between your start and end dates.

emddummiAuthor
Inspiring
March 25, 2019

Valid Dates are only monday to friday, because employees only work on These days.

I have a list of days which are not for work like 24.12. and so on

emddummiAuthor
Inspiring
March 27, 2019

Now i have a new code, but my field is empty

DatumTest = 26.03.2019

ATbisFertig = 10

function returnfinaldate() {

var holiday = ["2019/01/01","2019/04/19","2019/04/22","2019/05/01","2019/05/30","2019/06/10","2019/10/03","2019/12/24","2019/12/25","2019/12/26","2019/12/31"];

  var startDate = this.getField('DatumTest').value;

  var startDate = util.scand("dd/mm/yyyy");

  var endDate = new Date();

  var endDate = util.scand("dd/mm/yyyy");

  var noOfDaysToAdd = this.getField('ATbisFertig').value;

  var count = 0;

  endDate = startDate;

        while (count < noOfDaysToAdd) {

            endDate.setDate(endDate.getDate() + 1)

            if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) {

                count++;

            }

        }

        return endDate;

}

event.value = returnfinaldate();