These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Modification of existing script to calculate numbers of day

esdontan
Registered: May 1 2009
Posts: 19
Answered

Can anyone guide me how to modify below scripts in order reach objective of result excluding weekend and public holidays after adding 2 days on current date?

var strStart = this.getField("Today").value;
if(strStart.length)
{

var dateStart = util.scand("dd mmmm yyyy",strStart);
var oneDay = 24 * 60 * 60 * 1000;
var dueMillis = dateStart.getTime() + 2 * oneDay;
var dueDate = new Date(dueMillis);
event.value = util.printd("dd mmmm yyyy",dueDate);
}

Thanks a lot.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
It's gonna be tricky. The JS Date object does not have any information about public holidays. You will need to manually create an array of such dates and then scan this array each time you add a day to see if you've hit one of them. To ignore weekends you can use the getDay() method.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Weekends can be easily excluded because there is one each week, i.e., days = 5 * #ofWeeks + some special logic for handling the end cases. But for holidays you'll have to do what Try67 suggest and scan a special Holidays array to see which one fall into the time period. Count of the number of matching days and subtract from the total.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

esdontan
Registered: May 1 2009
Posts: 19
Thanks for your prompt reply Try67 and Thomp, please treat me as newbie. Care to guide me how to modify my script based on your advice? For you information, Try67, i cannot find anything from your suggested link that fit into my requirement.

Implementing array is perfectly ok since JS Date does not have information about public holiday.
esdontan
Registered: May 1 2009
Posts: 19
Hello gentleman,

I had modified my script but it not reflecting any view, please aide.

var ndate = this.getField("Text4");
nDate.value = util.printd("dd mmmm yyyy",new Date());

function nDate(nDays){
var date = new Date();
date.setDate(date.getDate() + nDays);
if(date.getDay()==6)
date.setDate(date.getDate() + 3);
else if(date.getDay()==5)
date.setDate(date.getDate() + 4);
return date;
}
esdontan
Registered: May 1 2009
Posts: 19
Hello gentlemen,

I had modified my script shown on below but it is not reflecting any value, please aide.



var ndate = this.getField("Text4");
nDate.value = util.printd("dd mmmm yyyy",new Date());

function nDate(nDays){
var date = new Date();
date.setDate(date.getDate() + nDays);
if(date.getDay()==6)
date.setDate(date.getDate() + 3);
else if(date.getDay()==5)
date.setDate(date.getDate() + 4);
return date;
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I think you are on the right track. If the start date is a thursday or Friday then the end day has to be in the next week. To check for a holiday you need a more general methodology and a way of representing the holidays that can be easily checked by the script. Since you have to check both of these things, and you are only dealing with 2 days, it might be easier use a loop to walk through every day after the start date and check to see if its a weekend or holiday. If it is ok then increment a counter, if it's not ok then move on to the next date. Keep it up untile the count reaches 2 days.

Something like this (in psuedocode)

nextDay =
var nCnt = 0;
do{
nextDay =
if( !NotaWeekend(nextDay) && !NotaHoliday(nextDay))
cnt++;
}while(cnt < 2);FinalDate =Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

esdontan
Registered: May 1 2009
Posts: 19
Finally the script is running and one last thing to do is creating the holiday array. Can you guys guide me how to create the array, sorry for me asking folly question?

var dtStart = new Date(this.getField("Today").value);
var nDayStart = dtStart.getDay()
var oneday= 24 * 60 * 60 * 1000;

if(nDayStart == 4 || nDayStart == 5)
{
nday = 4;
var newDate = dtStart.getTime() + nday * oneday;
var dueDate = new Date(newDate);
event.value = util.printd("dd mmmm yyyy",dueDate);
}
else if (nDayStart == 6)
{
nday = 3;
var newDate = dtStart.getTime() + nday * oneday;
var dueDate = new Date(newDate);
event.value = util.printd("dd mmmm yyyy",dueDate);
}
else
{
nday = 2;
var newDate = dtStart.getTime() + nday * oneday;
var dueDate = new Date(newDate);
event.value = util.printd("dd mmmm yyyy",dueDate);
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
A holiday array could be done in a number of ways. You'd want to arrange it to fit seemlessly into your algorithm for finding the end date. But here's one way you could do it.

An Array of arrays. The date object returns zero based month numbers so you can have an array of 12 arrays.

var aHolidays = [ [1,12],[14] .... etc]

So that "aHolidays[0]" is the holidays for January. It returns an array of 2 numbers, the 1st and the 12th.

You could also do it so that there was an array of all days for each month

var aHolidays = [new Array(31), new Array(28), etc.... ];

Then fill in the holiday dates.
aHolidays[0][0] = true; // Jan 1st
aHolidays[0][11] = true; // Jan 12th
aHolidays[1][13] = true; // Feb 14th

This sets up January 1st and 12th, and Febuary 14th to be Holidays, all the other days return "undefined", which translates to false in a logical expression.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

esdontan
Registered: May 1 2009
Posts: 19
You are professional and thanks for your advice.. How about the year??
How to create range of date to check holiday condition?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well if the dates are different for each year then you'll need a different array for each year. If you can algorithmically generate the Holiday dates you could writ a script that creates the Holiday array. But this is getting pretty complex.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

esdontan
Registered: May 1 2009
Posts: 19
var aHolidays = [new Array(31), new Array(28), etc.... ]; --- do not understand the approach

Then fill in the holiday dates.
aHolidays[0][0] = true; // Jan 1st
aHolidays[0][11] = true; // Jan 12th
aHolidays[1][13] = true; // Feb 14th


Can you explain what is the purpose of using 1 st stage of array? Why figure is 31 and 28?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
These are the number of days in each month, January is 31, February is 28 (assuming it's not a leap year), etc.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

esdontan
Registered: May 1 2009
Posts: 19
How to match the below arrays if there two holidays in one month?

var aHolidays = [new Array(31), new Array(28), etc.... ]; --- do not understand the approach

Then fill in the holiday dates.
aHolidays[0][0] = true; // Jan 1st
aHolidays[0][11] = true; // Jan 12th
aHolidays[1][13] = true; // Feb 14th
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Just how you did it should work fine.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

esdontan
Registered: May 1 2009
Posts: 19
The arrays should match with getDay()or getTime(), i presume should be getDay(). Correct me if i am wrong.