Skip to main content
lifeis2short
Participant
March 11, 2016
Question

Help with script to add business days to a date and display the new date in an Adobe form.

  • March 11, 2016
  • 2 replies
  • 893 views

 

There has got to be a very easy solution but I am stumped and can't figure it out or find anything online that answers it!  PLEASE HELP!

 

I'm working on a PDF where the user enters a date in field "DebriefDate."  I have a script that will populate a subsequent date field and add two days to the original date to provide a second due date for an additional step in the process.  What I need is for the script to ignore weekends.   

 

For example:

User types 03/11/2016 in the "DebriefDate" field :: I need the "d1" field to populate 03/14/2016" to take into account that 3/13 is a weekend date. 

 

Also, I am NOT concerned about holidays unless that is extremely simple to include.

 

This is the code I currently have:

 

// ---> start code

/* Create a date object containing the date from the DebriefDate field. */

var d1 = new Date(this.getField("DebriefDate").value);

/* num contains the numeric representation of the current date. */

var num = d1.valueOf();

/* Add two days to DebriefDate. */

/* 2000 ms / sec; 60 sec / min; 60 min / hour; 24 hours / day */

num += 2000 * 60 * 60 * 24;

/* Create our new date that is one day ahead of the current date. */

var d2 = new Date(num);

/* Print out the current date and our new date using util.printd */

  1. event.value =util.printd("mm/dd/yyyy", d2);

// ---> end code

Thank you in advance for all solutions!!    

This topic has been closed for replies.

2 replies

lifeis2short
Participant
March 16, 2016

Thank you for your input.  I am not getting an errors since what I have only adds two days to the date that is entered.   I am not experienced in JavaScript as it is has been years since I have had training and have not used extensively it so I your suggestions really don't make a lot of sense to me.   I had hoped I could Google some sample code and find something that was built to accommodate this.  It seems like a common sense calculation that many people would use if it was available.  Unfortunately, I had no luck finding anything similar so I thought I would try this forum to see if someone could help me with how to do it.  I will research the cFormat, CDate method but can you tell me if that will help me determine how to exclude Saturday and Sunday?

Inspiring
March 11, 2016

Are you getting any errors when adding the script?

Do you get any errors in the Acrobat JavaScript console when filling in the form?

The value of a field is "a.event.value = util.printd("mm/dd/yyyy", d2);"


The to change the value of the currently focused form field is to use "event.value = util.printd("mm/dd/yyyy", d2);".


Trying to use "new Date(someDAteValue)" is not always reliable because there are so many possible formats for a date. It is preferable to use the Acrobat JavaScript "util.scand(cFormat, cDate)" method.

I would also look at using the getDate() and setDate() JavaScript methods to advance the date object by 2 days.

var d1 = new Date(this.getField("DebriefDate").value);

/* num contains the numeric representation of the current date. */

var num = d1.valueOf();

/* Add two days to DebriefDate. */

/* 2000 ms / sec; 60 sec / min; 60 min / hour; 24 hours / day */

num += 2000 * 60 * 60 * 24;

/* Create our new date that is one day ahead of the current date. */

var d2 = new Date(num);

/* Print out the current date and our new date using util.printd */

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

// ---> end code

You may need to change the format image of the date string.