Adding number of days to date field

Jul 27, 2017

Copy link to clipboard

Copied

I am making a document that generates pay periods and the numbers get off when I get to Daylight Savings Time. When I enter the first date, the next box adds 13, the next adds 1 to start the next pay period, next 13, and so on. I am calculating this using milliseconds and was wondering if I can add something to disable DST or at least calculate is using days so this wouldn't be a problem. Can anyone help me?

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Use this code as the custom calculation script of the second field:

// Add one day

var s = this.getField("BPP").valueAsString;

if (s=="") event.value = "";

else {

    var d = util.scand("mm/dd/yy", s);

    d.setDate(d.getDate()+1); // replace 1 in this line with any other number you wish to add...

    event.value = util.printd("mm/dd/yy", d);

}

PS. I've developed a tool that allows you to set up such calculations easily and without having to write any code. It also has an option to automatically skip weekends and a custom-defined list of holidays. If you're interested you can find it here: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation

TOPICS
Acrobat SDK and JavaScript, Windows

Views

295

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Adding number of days to date field

Jul 27, 2017

Copy link to clipboard

Copied

I am making a document that generates pay periods and the numbers get off when I get to Daylight Savings Time. When I enter the first date, the next box adds 13, the next adds 1 to start the next pay period, next 13, and so on. I am calculating this using milliseconds and was wondering if I can add something to disable DST or at least calculate is using days so this wouldn't be a problem. Can anyone help me?

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Use this code as the custom calculation script of the second field:

// Add one day

var s = this.getField("BPP").valueAsString;

if (s=="") event.value = "";

else {

    var d = util.scand("mm/dd/yy", s);

    d.setDate(d.getDate()+1); // replace 1 in this line with any other number you wish to add...

    event.value = util.printd("mm/dd/yy", d);

}

PS. I've developed a tool that allows you to set up such calculations easily and without having to write any code. It also has an option to automatically skip weekends and a custom-defined list of holidays. If you're interested you can find it here: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation

TOPICS
Acrobat SDK and JavaScript, Windows

Views

296

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 27, 2017 0
Most Valuable Participant ,
Jul 27, 2017

Copy link to clipboard

Copied

Yes, this is a common problem. Instead of adding days using milliseconds you should use the setDate method. For example:

var d = new Date();

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

This code will add one whole day to the d variable, regardless of the hours.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0
Jul 27, 2017

Copy link to clipboard

Copied

So the only input the end user will be doing with the dates is the box "BPP". How would I incorporate this into the script you gave me? I'm sorry, this is all new to me!

1.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0
Most Valuable Participant ,
Jul 27, 2017

Copy link to clipboard

Copied

How many days do you want to add, and what's the format of the dates?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0
Jul 27, 2017

Copy link to clipboard

Copied

MM/DD/YY, in one box I want to add 1, in another box I want to add 13. If I can get the first line I can figure out the rest of the rows!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0
Jul 27, 2017

Copy link to clipboard

Copied

Basically this is what I was using. I found it online and changed only the things I had to.

 

event.value = "";

var strDate = this.getField("Pay Period WeekRow1_2").value;

{

  var oDate = util.scand("mm/dd/yy",strDate);

  if(oDate)

  {

    var fiveDays = 1 * 24 * 60 * 60 * 1000;

    oDate = new Date(fiveDays + oDate.getTime());

    event.value = util.printd("mm/dd/yy", oDate);

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0
Most Valuable Participant ,
Jul 27, 2017

Copy link to clipboard

Copied

Use this code as the custom calculation script of the second field:

// Add one day

var s = this.getField("BPP").valueAsString;

if (s=="") event.value = "";

else {

    var d = util.scand("mm/dd/yy", s);

    d.setDate(d.getDate()+1); // replace 1 in this line with any other number you wish to add...

    event.value = util.printd("mm/dd/yy", d);

}

PS. I've developed a tool that allows you to set up such calculations easily and without having to write any code. It also has an option to automatically skip weekends and a custom-defined list of holidays. If you're interested you can find it here: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 1
Jul 27, 2017

Copy link to clipboard

Copied

Awesome, that worked great. Thank you so much!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2017 0