Skip to main content
Participant
June 27, 2016
Answered

Possible to auto-calculate date 2 weeks past user-entered date in form?

  • June 27, 2016
  • 2 replies
  • 971 views

Hello! I'm trying to create a form that auto populates a few dates based on a user-entered value. For example, if a user enters a due date of 7/1/16, I would like another field to auto-populate with 7/15/16 (2 weeks from the previously entered date). Is this possible? I've been searching for scripts but no luck so far.

Thanks!

Melissa

This topic has been closed for replies.
Correct answer try67

Sure, it's possible. Let's say the name of the date field entered by the user is Date1. You can use this script as the custom calculation script of the other field.

var dateString = this.getField("Date1").valueAsString;

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

else {

    var d = util.scand("m/dd/yy", dateString);

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

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

For a tool that allows you to set up such calculations easily, see: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation

2 replies

Karl Heinz  Kremer
Community Expert
Community Expert
June 27, 2016

Date calculations in Acrobat's JavaScript implementation are done the same way as in every other JavaScript system: The "Date" object is part of the JavaScript core language. You can find out more about the Date object here: Date - JavaScript | MDN

You can find tutorials about how to use the Date object in Acrobat here: https://acrobatusers.com/tutorials/date_time_part1

The key is to use the getDate()/setDate()  methods: Let's assume you get a date from field "Date1", and set the new date in "Date2", you can use something like this as the custom calculation script of the Date2 field:

var dateStr = this.getField("Date1").value;

if (dateStr != "") {

  var theDate = util.scand("mm/dd/yyyy", dateStr);

  theDate.setDate(theDate.getDate() + 14);

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

}

else {

  event.value = "";

}

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 27, 2016

Sure, it's possible. Let's say the name of the date field entered by the user is Date1. You can use this script as the custom calculation script of the other field.

var dateString = this.getField("Date1").valueAsString;

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

else {

    var d = util.scand("m/dd/yy", dateString);

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

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

For a tool that allows you to set up such calculations easily, see: Custom-made Adobe Scripts: Acrobat -- Apply Automatic Date Calculation

Participant
June 27, 2016

Thank you!!!