Expiry Date
How to check a date entered is greater than current date? i.e. not expired
How to check a date entered is greater than current date? i.e. not expired
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");
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.