Javascript assistance for clearing field

New Here ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

Hello Adobe Community! I'm trying to calculate a date field to add an extra day based on the input in the original field. This is simple enough, however I would like the calculated field blank if the origin field is also blank. I seem to not be able to figure this out. What I have is this:

 

var date=new Date(this.getField("prodDate").value);

if (!date) {
event.value = date;
} else {
date.setDate(date.getDate()+1);
event.value=util.printd("mm/dd/yy", date);

}

 

In the above, a date entered calculates correctly, but when I remove the entry to the origin field the calculated field displays "01/00/00". Any direction on what I need to do to fix this?

 

Thanks!

TOPICS
General troubleshooting , JavaScript

Views

29

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 , Aug 18, 2022 Aug 18, 2022

You need to check the value of the field directly, not the date object, because new Date("") returns an Invalid Date object, not null. So use 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);
}

Likes

Translate

Translate
Community Expert ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

You need to check the value of the field directly, not the date object, because new Date("") returns an Invalid Date object, not null. So use 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);
}

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 ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

LATEST

Genius! Thank you so much! I'm still super new at JS but this makes total sense. So my original code was always returning a value and thus doing the calculation even with no field, thus returning a useless date.

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