Skip to main content
Participant
October 31, 2020
解決済み

Trying to define a period of time as qualification for a user defined date

  • October 31, 2020
  • 返信数 1.
  • 803 ビュー

Im trying to return either "Qualified" or "Too early for qualification" from a user defined date. 

Qualification occurs if the user defined date is gretaer than a specified date.

I have limited scripting knowledge but I am missing something

 

var 1A = (this.getField("installation date").value);

if 1A> date (2017,5,19) event.value = "Qualified";

else if 1A< date (2017,5,19) event.value = "Too early for qualification";

}

Can anyone help?

このトピックへの返信は締め切られました。
解決に役立った回答 ls_rbls

You can try it out like this:

 

 

var A1 = this.getField("installation date").valueAsString;

if (A1.length !=="") {

  var userDefinedDate = util.scand("yyyy,m,dd", A1);
  var specifiedDate = "2017,5,19"
  var QualificationDate = util.scand("yyyy,m,dd", specifiedDate)


if ( userDefinedDate.getTime() <=  QualificationDate.getTime() ) {event.value = "Too early for qualification";}

if ( userDefinedDate.getTime() > QualificationDate.getTime() ) {event.value = "Qualified";}
} 

if (A1 =="") event.value ="";

 

 

You need to use the scand method first in order to convert the date string value  to an date object.  This, of course, can be achieved in many ways using JavaScript date arithmetic. But in my example above I preferred to parse the date arithmetic by converting the defined date string ("2017,5,19") to an date object and compare the difference between two date objects in milliseconds. In my personal opinion, and as I continue to learn JavaScript scripting, this method provides a very accurate results (but I am sure there are other ways too).

 

This is referenced in greater detail in the Adobe Acrobat DC SDK, Using JavaScript in Forms, 
Developing Acrobat® Applications Using JavaScript™,  "Date arithmetic", Page 94

 

Specifically in the following section:

 

  • "  Date arithmetic
    It is often useful to do arithmetic on dates to determine things like the time interval between two dates or what the date will be several days or weeks in the future. The JavaScript Date object provides several ways to do this. The simplest and possibly most easily understood method is by manipulating dates in terms of their numeric representation. Internally, JavaScript dates are stored as the number of milliseconds (one thousand milliseconds is one whole second) since a fixed date and time. This number can be retrieved through the valueOf method of the Date object. The Date constructor allows the construction of a new date from this number."

返信数 1

ls_rbls
Community Expert
ls_rblsCommunity Expert解決!
Community Expert
October 31, 2020

You can try it out like this:

 

 

var A1 = this.getField("installation date").valueAsString;

if (A1.length !=="") {

  var userDefinedDate = util.scand("yyyy,m,dd", A1);
  var specifiedDate = "2017,5,19"
  var QualificationDate = util.scand("yyyy,m,dd", specifiedDate)


if ( userDefinedDate.getTime() <=  QualificationDate.getTime() ) {event.value = "Too early for qualification";}

if ( userDefinedDate.getTime() > QualificationDate.getTime() ) {event.value = "Qualified";}
} 

if (A1 =="") event.value ="";

 

 

You need to use the scand method first in order to convert the date string value  to an date object.  This, of course, can be achieved in many ways using JavaScript date arithmetic. But in my example above I preferred to parse the date arithmetic by converting the defined date string ("2017,5,19") to an date object and compare the difference between two date objects in milliseconds. In my personal opinion, and as I continue to learn JavaScript scripting, this method provides a very accurate results (but I am sure there are other ways too).

 

This is referenced in greater detail in the Adobe Acrobat DC SDK, Using JavaScript in Forms, 
Developing Acrobat® Applications Using JavaScript™,  "Date arithmetic", Page 94

 

Specifically in the following section:

 

  • "  Date arithmetic
    It is often useful to do arithmetic on dates to determine things like the time interval between two dates or what the date will be several days or weeks in the future. The JavaScript Date object provides several ways to do this. The simplest and possibly most easily understood method is by manipulating dates in terms of their numeric representation. Internally, JavaScript dates are stored as the number of milliseconds (one thousand milliseconds is one whole second) since a fixed date and time. This number can be retrieved through the valueOf method of the Date object. The Date constructor allows the construction of a new date from this number."
try67
Community Expert
Community Expert
October 31, 2020

Small nitpick... Change this line:

if (A1.length !=="") {

To:

if (A1!=="") {

ls_rbls
Community Expert
Community Expert
October 31, 2020

Thank you !