Skip to main content
Participating Frequently
February 14, 2017
Answered

Autofull date that compensates for February --adjust current script or rewrite if needed. i have included the script below.

  • February 14, 2017
  • 2 replies
  • 1497 views

Hello All,

I have read a number of posts on several sites. Additionally have noted a number of knowledgeable folks supporting all the of the newb questions. I have visited some resource sites like adobe. and various forums like this one.
However I am still not able to glean the solution for the correct java script that will address correctly auto populating the  Month of February if 1.29 thru 1.31 is selected in the initial date field.
all other dating works correctly
Form has 3 date fields, I am trying to provide script that will allow my users to populate the first field and the 2nd and 3rd field will be dated 1 month later , naturally Feb. is an exception
the main script I have used to this point is
// get value of field to update - field name is Text5
var sDate = this.getField('Text5').value;
// convert to date time object
// the fields format is mm/dd/yyyy
var oDate = util.scand('mm/dd/yyyy', sDate);
// get the parts of the date
var fFullYear = oDate.getFullYear(); // 4 digit year
var fMonth = oDate.getMonth(); // zero based month
var fDate = oDate.getDate(); // date of month
// increment the month
fMonth++;
// create new date time object
var oNewDate = new Date(fFullYear, fMonth, fDate);
// set field value - field has no format
// formatting is applied by the printd method

event.value = util.printd('mm/dd/yyyy', oNewDate);
I have tried a couple others that I could share if needed, however the result of those scripts is the same...I can share the other variations of java scripts used in further posts.

Any assistance in creating the additional scripting needed would be Greatly appreciated.

Ps I have tried tweaking this with formula for adding or subtracting days but alas not doing it correctly.

Again Thank you in advance

Brian

[ Mod: Email address removed ]

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Actually I misspoke, 31st will cause a month leap as well, e.g. 3.31 will give 5.01 in the next field


How are you doing with your solution? If you don't have one yet, I modified your script and added a check for the length of the month. I also corrected the problem with wrapping around from December to January, and I added support for leap years:

function isLeapYear(year) {     // determines if a given year is a leap year

  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);

}

var daysInMonth = [

  [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ], // non-leap year

  [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] // leap year

];

// get value of field to update - field name is Text5

var sDate = this.getField("Text5").value;

// convert to date time object

// the fields format is mm/dd/yyyy

var oDate = util.scand('mm/dd/yyyy', sDate);

// get the parts of the date

var fFullYear = oDate.getFullYear(); // 4 digit year

var fMonth = oDate.getMonth(); // zero based month

var fDate = oDate.getDate(); // date of month

// increment the month

fMonth++;

if (fMonth == 12) {

  fMonth = 0;

  fFullYear++;

}

if (fDate > daysInMonth[isLeapYear(fFullYear) ? 1 : 0][fMonth]) {

  fMonth++;

  fDate = 1;

}

// create new date time object

var oNewDate = new Date(fFullYear, fMonth, fDate);

// set field value - field has no format

// formatting is applied by the printd method

event.value = util.printd('mm/dd/yyyy', oNewDate);

2 replies

Karl Heinz  Kremer
Community Expert
Community Expert
February 14, 2017

What exactly should the behavior be when somebody enters e.g. 1/31/2017?

Participating Frequently
February 14, 2017

when January 29, 30 or 31 is entered the next field is populated with March 1,2 and 3 respectively.
I need the date to auto fill to February 1 for jan. 29 30 and 31.
and Karl , thank you for asking what the issue is , as the response from JR_Boulay is helpful I am not sure how to
put Joel's script to work.
you can see I have a basic script already. If that is wrong or needs editing please feel free to let me know
I am not sure how to proceed with the work I have presently

Again thank you to all who have offered assistance.
I am a newb at this
Brian H

JR Boulay
Community Expert
Community Expert
February 14, 2017

Hi.

You dreamed of, Joel made it for you!
dateExtended Library for Adobe Acrobat: http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/

.

Acrobate du PDF, InDesigner et Photoshopographe