Skip to main content
This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Here is the code I put in (code is located in the validation script box of "DateField" which has date format m/d/yyyy),

var curDoc = app.doc;

var date1 = curDoc.getField("DateField");

var date1Value = date1.value;

var actualDate1 = new Date(date1Value);

var curDate = new Date();

// some useful information:

console.show();

console.clear();

console.println("Inputted date: " + date1Value);

console.println("Actual date object: " + actualDate1);

console.println("Current date object: " + curDate);

console.println("Test result: " + String(actualDate1 > curDate));

console.println( "Actual date full year: " + actualDate1.getFullYear());

console.println("Current date full year: " + curDate.getFullYear());

console.println("Inputted date 2 digit year: " + actualDate1.getYear());

console.println("Current date 2 digit year: " + curDate.getYear());

// end of some useful information;

if(actualDate1 > curDate) {

console.println("yeah");

}

else {

console.println("boo");

}

here is the message I got back on the console

Inputted date: 5-2-2010
Actual date object: Invalid Date
Current date object: Mon May 14 2018 14:11:51 GMT-0500 (Central Daylight Time)
Test result: false
Actual date full year: NaN
Current date full year: 2018
Inputted date 2 digit year: NaN
Current date 2 digit year: 118
boo

TypeError: getField("DifferentField") is null
1:Field:Calculate

I double checked spelling of my form field, I even copied the field name and pasted it into the script.


Use Date.getFullYear() instead of getYear(): Date.prototype.getFullYear() - JavaScript | MDN

This will get rid of the 2 digit year problem you are seeing ("118"). The reference to DifferentField is very likely in a different script. Check all your scripts to see which one is using the name of an invalid field.

It's not a good idea to reference the field your validation script belongs to via the getField() method. All the information you need is available via the event data structure - e.g. event.value as the entered value.

app.doc is not documented. Use 'this' instead to get a reference to the current document - but in this case, because we can use event, that's not even necessary.

You may have to use the until.scand() method to parse your date string of mm-dd-yyyy

The following should work:

var date1Value = event.value;

var actualDate1 = util.scand("mm-dd-yyyy", date1Value);

var curDate = new Date();

// some useful information:

console.show();

console.clear();

console.println("Inputted date: " + date1Value);

console.println("Actual date object: " + actualDate1);

console.println("Current date object: " + curDate);

console.println("Test result: " + String(actualDate1 > curDate));

console.println( "Actual date full year: " + actualDate1.getFullYear());

console.println("Current date full year: " + curDate.getFullYear());

console.println("Inputted date 2 digit year: " + String(actualDate1.getFullYear()).substring(2,4));

console.println("Current date 2 digit year: " + String(curDate.getFullYear()).substring(2,4));

// end of some useful information;

if(actualDate1 > curDate) {

     console.println("yeah");

}

else {

     console.println("boo");

}

2 replies

Participant
May 13, 2018

Hello folks!

I've a similar question, that is: how can I make a field that automatically print current date when printing the doc?

Karl Heinz  Kremer
Community Expert
Community Expert
May 13, 2018

Use the “document will print” document action to set the contents of your field to the current date, and potentially the “document did print” action to clear the field again if you don’t want to show the date in the viewer. I would also save  state of the Doc.dirty property and reset it in document did print. This prevents the prompt to save the document when you close it without any other changes.

Participant
May 14, 2018

Hi Karl.

Could you be a little more precise? Especially on where I can find the action "document will/did print", becouse I've tried to find it, but there was no way... I talking about Acro Pro Dc

Thanks

BarlaeDC
Community Expert
Community Expert
May 8, 2018

HI,

you should be able to use something like this.

var curDoc = app.doc

// this is the date field the user enters into ( could be a text field, as long as we know the format.

var date1 = curDoc.getField("DateField");

var date1Value = date1.value;

// actually get date objects

var actualDate1 = new Date ( date1Value);

var curDate = new Date();

// check if actualDate1 is greater (in the future) than curDate

if ( actualDate1 > curDate)

{

console.println ( "yeah");

}

else

{

console.println ( "boo");

}

Hope this helps

Malcolm

Inspiring
May 8, 2018

To get the the value of a field one needs to provide the object name a "." and then property "value".

So to get the value of the value of "DateFiled" whose object name is "date1" one needs to write "datge.value".

You should be aware that the Date "new Date( somevalue )" will only work with a limited number of date formats and may misinterpret some dates. For example 01/02/2018 can be either January 2, 2018 or February 1, 2018 depending upon were one is in the world.