Validate date fields

New Here ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Good day Adobe community. I'm working on a form in which a user provides a production date for one of my labs. This input field is using date format (mm/dd/yy) and then has a second field which calculates an expiration date based on the input date. In almost all cases, what I have works, however I run into an issue that if a person manually enters a date (08/31 for example) without including the year, the production date finishes filling in just fine (08/31/22) but the calculated expiration date will not calculate the expiration date correctly and instead return 01/00/00.

 

Capture.PNG

 

I'm not sure what the best way to fix this is. My thought was to validate that if someone enters infomation into the production date to ensure they include the full format (mm/dd/yy) but this seems to involve regular expressions which a friend of mine who does JS says is greek to most developers and all the examples I find validate the field as mm/dd/yyyy. I of course don't know what I'm looking at and how to modify it for mm/dd/yy. I'm also open correcting whatever is causing the bad date calulation too!

 

The script I'm running to calculate is this:

var dateString = this.getField("prodDate").valueAsString;
if (dateString=="") event.value = ""; 
else {
	var date=new Date(dateString);
	date.setDate(date.getDate()+1);
	event.value=util.printd("mm/dd/yy", date);
}

This will display the expiration date field as blank if nothing is in the production date field and if something is entered will calculate the expiration date (A whole 1 day after).

 

Thanks in advance for the assist!

TOPICS
JavaScript , PDF forms

Views

31

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 01, 2022 Sep 01, 2022

Replace this:

var date=new Date(dateString);

With this:

var date=util.scand("mm/dd/yy", dateString)

If no year is specified, it will assume it's the current one.

Likes

Translate

Translate
Community Expert ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Replace this:

var date=new Date(dateString);

With this:

var date=util.scand("mm/dd/yy", dateString)

If no year is specified, it will assume it's the current one.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

LATEST

This worked perfectly! Thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines