Skip to main content
Participant
December 22, 2017
Answered

Javascript to populate dropdown field to other fields with staggered dates

  • December 22, 2017
  • 3 replies
  • 1206 views

Hi,  I am trying to tweak a timesheet that I have added a dropdown box that has dates entered into the <Options>, <Item List> as "1/6/2018" and every 14 days til the end of the year. Where I am stuck is trying to get the javascript that will take the chosen 'Pay Period Ending' date and add to the 14th date field. Then I am trying to get all the other fields to start from this date -1 so that each date of the two-week period is shown. This way, the dropdown date will automatically update the pay period dates. I made some progress following these instructions https://answers.acrobatusers.com/How-autofill-date-field-based-field-selection-q286091.aspx and got the end date to reflect my date but could not get subsequent fields to -1 from the other for the staggered dates I need. The only other issue was date format in that drop downs are MM/DD/YYYY  but the 14 date fields for column hours tally are too small and need to be MM/DD. Thank you.

This topic has been closed for replies.
Correct answer try67

You can use something like this as the custom validation script of your drop-down (I can't make out the field names, so I'm using generic ones):

var d = util.scand("mm/dd/yyyy", event.value);

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

this.getField("Date1").value = util.printd("mm/dd", d);

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

this.getField("Date2").value = util.printd("mm/dd", d);

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

this.getField("Date3").value = util.printd("mm/dd", d);

// etc.

3 replies

Participant
December 22, 2017

Thanks Try67!! This worked like a champ. I didn't know that a validation field could cause other fields to be populated with specific data so this was enlightening. I entered your script into my dropdown validation script section after renaming my indiscernible field names to your "Date1", "Date2" and so forth. I did have to modify the initial -1 since the "Pay period end" date would be the same date as the chosen drop down date. Once I choose my dropdown option and click anywhere else, all the date range cells automatically update to the desired range. Awesome! Thank you very much.

Thom Parker
Community Expert
Community Expert
December 22, 2017

Here's an article on this exact topic:

https://acrobatusers.com/tutorials/date_time_part2

It includes a time-sheet example

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 22, 2017

You can use something like this as the custom validation script of your drop-down (I can't make out the field names, so I'm using generic ones):

var d = util.scand("mm/dd/yyyy", event.value);

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

this.getField("Date1").value = util.printd("mm/dd", d);

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

this.getField("Date2").value = util.printd("mm/dd", d);

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

this.getField("Date3").value = util.printd("mm/dd", d);

// etc.