Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

javascript - Calculating date based on independent form field in PDF Form

I want to calculate a date that is exactly 5 days prior to a date chosen in a separate date field in a PDF Form. For example, if I choose "July 10, 2021" in Date1, I want Date2 to automatically populate with "July 5, 2021".

I know how to grab the date from field 1 and populate field 2 with it, and I sort of know how to do a 5 day (ago) calculation. The part I'm really struggling with is putting those two things together in the document. If I wanted to calculate 5 days ago from the current date, I can do that no problem as it's documented all over planet earth and the internet lol. But calculating 5 days ago from a selected date in another field is throwing me for a loop.

I'm not well versed in JavaScript, but I'm also wondering if this might be a solution:

//turning this piece of code    
event.value=this.getField("Date1").value;

//into this piece of code
var d = this.getField("Date1").value;

Since I don't need the second date field to actually populate with the first date field value, I'm wondering if it's as simple as turning the first field value into a variable and then dropping the variable into the calculation script under "Custom calculation script" in the form field's properties.

question from:https://stackoverflow.com/questions/65846602/calculating-date-based-on-independent-form-field-in-pdf-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

While I can't help you with the PDF form item setting/getting, here is how to add or subtract days from a date in JavaScript, using your date string format "MMM DD, YYYY":

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];

// convert "MMM, DD, YYYY" string to Date object
function makeDateFromStr(str) {
  const strNoComma = str.split(',').join('');
  const [monthStr, day, year] = strNoComma.split(' ');
  const monthIndex = months.indexOf(monthStr);
  return new Date(year, monthIndex, day);
}

// add/subtract number of days to Date object,
// returning new date
function addDaysToDate(date, addDays) {
  const currDays = date.getDate();
  const newDate = new Date(date.getTime());
  newDate.setDate(currDays + addDays);
  return newDate;
}

// convert Date object to string "MMM DD, YYYY"
function myDateFormat(date) {
  const monthStr = months[date.getMonth()];
  const day = date.getDate();
  const year = date.getFullYear();
  return `${monthStr} ${day}, ${year}`;
}

const date1Str = "Jul 10, 2011";
console.log(`date1Str: "${date1Str}"`);

const date1 = makeDateFromStr(date1Str);
console.log('date1:',date1.toISOString());

const date2 = addDaysToDate(date1, -5);
console.log('date2:',date2.toISOString());

const date2Str = myDateFormat(date2);
console.log(`date2Str: "${date2Str}"`);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...