Copy link to clipboard
Copied
I have 1 field titled "Today" that populates the current date. Then I have a drop-down field titled "Release Date" with the following selections: 14 Days, 180 Days, and Permanent. I am trying to change the "Release Date" field into a calculated date using the "Today" field depending on what is selected in the drop-down list. Example: If the "Today" field reads 01/18/2016 and the user selects "14 Days" from the drop-down list as the "Release Date" then I want the text to show as "02/01/2016", which is 14 days from today's date, instead of showing as "14 Days". What script should I use?
Copy link to clipboard
Copied
If you want the date calculated, you will need to perform the calculation using custom JavaScript for the calculation.
Have you looked for scripts about adding # days to a given date?
The Acrobat JavaScript API Reference Manual has examples of date calculations. Use Date Objects
I would rather use JavaScript's getDate and setDate options for manipulating days.
Copy link to clipboard
Copied
I'm new to this and have been reading so many discussions and verbiage on this that I am confusing myself more than anything. This is the current custom calculation script I have:
(function () {
// Get date from field
var v = getField("Today").value;
if (v) {
// Convert string to date
var d = util.scand('mm/dd/yyyy', v);
// Add 14 days
d.setDate(d.getDate() + 14);
// Set value of this field to the new date
event.value = util.printd("mm/dd/yyyy", d);
} else {
// Blank field if no date entered
event.value = "";
}
})();(function () {
// Get date from field
var v = getField("Today").value;
if (v) {
// Convert string to date
var d = util.scand('mm/dd/yyyy', v);
// Add 180 days
d.setDate(d.getDate() + 180);
// Set value of this field to the new date
event.value = util.printd("mm/dd/yyyy", d);
} else {
// Blank field if no date entered
event.value = "";
}
})();
The issue I'm having is no matter which option is selected from the drop-down list, it still populates the date as Today + 180 days.
Copy link to clipboard
Copied
I see nothing that checks the dropdown selection for the number of days to use in the calculation.
Copy link to clipboard
Copied
Well then I guess I need to research scripts more on how to make that happen because this is Greek to me. Thanks for your help.
Copy link to clipboard
Copied
Document level Scripts:
// common functions for processing PDF forms;
// get a field object with error processing;
function GetField(cName) {
// return field object or null if not found;
var oField = this.getField(cName);
if(oField == null) {
app.alert("Error accessing field named " + cName, 0,1);
}
return oField;
} // end GetField function;
// scan date string with a given format to a date object;
function Scand(cFormat, cValue) {
var oDate = util.scand(cFormat, cValue);
if(oDate == null) {
app.alert("Error converting date string " + cValue + " with a format of " + cFormat, 0, 1);
}
return oDate;
} // end Scand function;
// Add current date to form field;
function Now(cFormat, cName) {
var bSuccess = false;
var oField = GetField(cName);
if(oField != null) {
oField.value = util.printd(cFormat, new Date());
bSuccess = true;
}
return bSuccess;
}
// populat Today field;
Now("mm/dd/yyyy", "Today");
// end documeht level scripts;
// custom calculation script for field to dispaly the release date;
// get the open date for the form;
var oToday = GetField("Today");
// get the number of days to add'
var oAdjustDays = GetField("ReleaseDate");
// default value for field;
event.value = oAdjustDays.value;
// could add some code for a value of " ";
// add days if AdjustDays value is a number;
if(oAdjustDays.value != " " && isNaN(oAdjustDays.value) == false) {
// convert Today to date object;
var oBaseDate = Scand("mm/dd/yyyy", oToday.value);
// adjsut for selected nuber of days;
oBaseDate.setDate(oBaseDate.getDate() + oAdjustDays.value);
// populate date value;
event.value = util.printd("mm/dd/yyyy", oBaseDate);
}
// end custom calculation script;
Copy link to clipboard
Copied
Ok, so the customer doesn't want their users to have to select "14 Days" or "180 Days" from the drop-down list. They want the dates (Today's date + 14 days and Today's date + 180 days) to populate as selections in the drop-down list, along with the option of "Permanent". My head is spinning. Example: Today's Date: 01/19/2016; Release Date: (3 options to select from) 02/02/2016 (which is today's date + 14 days), 07/17/2016 (which is today's date + 180 days), Permanent.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now