Skip to main content
Known Participant
July 27, 2025
Answered

Populate text field with date based on date entered in another text field

  • July 27, 2025
  • 1 reply
  • 367 views

I have two text fields - Text1 and Text2.  Both are fomatted for dates in m/d/yy format.  When I enter a date into Text1, I would like Text2 to populate with a date that is eleven months later, plus enough days to go to the end of that month.  For example, if 7/17/25 is entered into Text1, I would like Text2 to populate with 6/30/26.  Since months don't end on the same day (28th, 29th, 30th, 31st), I'm not sure if this is possible.  TIA for any help.

Correct answer Thom Parker

I would do a small variation on Try67's solution.  Construct a new date that is 12 months out, with the date(day of month) set to 0.  12 months out will always be the same month plus 1 year.  Month numbers in date objects are zero based, so the to get the same month, subtract 1 from the month number in the formatted date.  In the constructor for the date object, day zero of the month is the last day of the previous month.

 

Like this:

// first parse date
if(/(\d{1,2})\/\d{1,2}\/(\d{1,2})/.test(this.getField("Text1").value))
{// next, construct the new date
    var date12MonthsOut = new Date(2000 + Number(RegExp.$2) + 1, Number(RegExp.$1)-1, 0);
    event.value = util.printd("m/dd/yy",date12MonthsOut);
}

 

1 reply

try67
Community Expert
Community Expert
July 27, 2025

It's possible, but tricky, because of the reason you mentioned. I would change the date to the first of the month, first of all, otherwise you might end up in the wrong month when adding 11 months to the original date. Then use a loop to add one day at a time, until you reach next month, then deduct one day to go back to the last day of the previous one.

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 27, 2025

I would do a small variation on Try67's solution.  Construct a new date that is 12 months out, with the date(day of month) set to 0.  12 months out will always be the same month plus 1 year.  Month numbers in date objects are zero based, so the to get the same month, subtract 1 from the month number in the formatted date.  In the constructor for the date object, day zero of the month is the last day of the previous month.

 

Like this:

// first parse date
if(/(\d{1,2})\/\d{1,2}\/(\d{1,2})/.test(this.getField("Text1").value))
{// next, construct the new date
    var date12MonthsOut = new Date(2000 + Number(RegExp.$2) + 1, Number(RegExp.$1)-1, 0);
    event.value = util.printd("m/dd/yy",date12MonthsOut);
}

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
July 28, 2025

Works great.  Thanks.  It even accounts for leap year.  If I enter a date for March of 2026 into Text1, it returns 2/28/27 in Text2.  If I enter a date for March of 2027 in Text1, it returns a date of 2/29/28 in Text2.