Copy link to clipboard
Copied
How to check a date entered is greater than current date? i.e. not expired
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.v
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I have tried this, and I am still not getting any message when a date input is expired. The field is a date field with format m/d/yy
Copy link to clipboard
Copied
I would start by looking at the Acrobat JavaScript console for any errors and then print to the console some of the values that I am using in the script.
Try the following script:
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");
}
You will see an issue with the use of the 2 digit year. See MDN JavaScript Reference Date object for more information about the 2 digit year. This explains how the Millennium bug was handled in JavaScript.
Copy link to clipboard
Copied
I changed my date format to m/d/yyyy.
The date I put in the field to try the code was 5/2/2010
When I input the code above my feed back is:
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
Copy link to clipboard
Copied
The error message is an indication for an invalid field name. There is no field named "DifferentField". Is the spelling correct?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
Make sure you are spelling and capitalizing the field names exactly as the form field's name has been entered.
Copy link to clipboard
Copied
Hello folks!
I've a similar question, that is: how can I make a field that automatically print current date when printing the doc?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi,
From the JavaScript Toolbar, select "Document Actions", then pick the one you want to use from the dialog.
Regards
Malcolm
Copy link to clipboard
Copied
You can always use the tools search function for functionality you know is there, but don't know where to find it. Just type "action" into the search field, and Acrobat will - among other things - return with a link to the "JavaScript>Document Actions" item. Once you start that, you can then - just as Malcolm explained - pick one of the actions, and edit it.
Copy link to clipboard
Copied
It is available under 2 tools. First you can add document actions while editing forms with the "Prepare form" tools. It is under the "More" option. It is also available under the "JavaScript" tool. And as, noted when all else fails, use the "search" feature.