These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Date validation

die488
Registered: Nov 21 2011
Posts: 5
Answered

Hi there,
 
I'm new to forms with Acrobat Pro and I'd like to script a validation on a date which only accepts introduced_date > (current_Date +10_days).
Format is dd/mm/yyyy
I tried with :
 
var d1 = new Date();
var d2 = getField("Evénement_date").value;
if ((d2 - d1) < 10 )
{
app.alert("Error");
}
 
but it does not work :(
 
Thanks in advance for any help

My Product Information:
Acrobat Pro 9.3, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can not treat the character date string like a number, it is alphabetic, special text characters and numbers. You need to convert the date string to a number for all dates and then perform the comparison.

There is a seires of tutorials on date and time calculations on this site.

George Kaiser

die488
Registered: Nov 21 2011
Posts: 5
Thank you for pointing me the direction but I can't sadly get it working, this is where I am currently :

var rightNow = new Date(); //current date
var msRightNow = rightNow.getTime(); //convert in ms

var dateDemandee = this.getfield("Date"); //the date to evaluate
var msdateDemandee = dateDemandee.getTime(); //in ms

var delaiMin = 10 * 24 * 60 * 60 * 1000; //10 days in ms

var dateMin = rightNow + delaiMin;//minimal date from now to accept

if(msdateDemandee < dateMin)//test
{
app.alert("Error");
}

The problem seems (via the console) to be related to this.getfield(); but why ?

Thank you
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You need to access the value of the field, not just the field itself.
Also, you need to convert this value to a Date object from a string.

Change this:
var dateDemandee = this.getField("Date"); //the date to evaluate
To this:
var dateDemandee = new Date(this.getField("Date").value); //the date to evaluate

Alternatively, you can use the util.scand() method to convert the string value to a Date object.

EDIT: fixed the capitalization errors pointed out by George...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to spell and capitalize objects, properties, methods, etc correctly.

You have not converted the date string to the date object and the date object to the number of milliseconds from the Epoch date.

You also need to ajdust for the inclusion of the start and end point days.

// get current date time
var oRightNow = new Date(); //current date
// convert to milliseconds from Epoch data
var nRightNow = oRightNow.getTime(); //convert in ms
// convert milliseconds to days
var nTodayDays = Math.floor(nRightNow / (1000 * 60 * 60 * 24));

// show some information
console.println("Today:");
console.println("current date object: " + oRightNow);
console.println("milliseconds since Epoch date: " + nRightNow);
console.println("Days since Epoch date: " + nTodayDays);

// get imputed date string
var sDateDemandee = this.getField("Date").value; //the date to evaluate
// convert to date time object - adjust format of date string as needed
var oDateDemandee = util.scand("mm/dd/yyyy", sDateDemandee);
// convert to days
var nDateDemandee = Math.floor(oDateDemandee.getTime() / (1000 * 60 * 60 * 24) );

// show some information about date:
console.println(" ");

console.println("Imputed date:");
console.println("Date string: " + sDateDemandee);
console.println("Date object: " + oDateDemandee);
console.println("Date days since Epoch: " + nDateDemandee);

// compute difference
console.println("");
console.println("Difference in days including endpoints: " + (1 + nDateDemandee - nTodayDays) );

// test for difference less than 10 days
if((1 + nDateDemandee - nTodayDays) < 10) {
app.alert("Error");
}

If you use this script as custom validation script for the "Date" field, you will need to change the references to the "Date" field to the the 'event'.


George Kaiser

die488
Registered: Nov 21 2011
Posts: 5
Thank you,
It mostly work...

The validation have a delay of one try (sorry for my bad english, I explain) :
(in dd/mm/yyyy - current date is 22/11/2011 - 10 days is the minimum delay)

1: 24/11/2011 - no Error ( should be )
2: 25/11/2011 - error ( OK but console says "Difference in days including endpoints: 2" --> 1: )
3: 15/12/2011 - error ( NOK, console says "Difference in days including endpoints: 3" --> 2: )
4: 31/12/2011 - no error (OK but in console "Difference in days including endpoints: 23" --> 3: )
5: 23/11/2011 - no error (NOK, console "Difference in days including endpoints: 39" --> 4: )
and so on

We are close, thank you in advance
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Probably a problem with the field calculation order.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

die488
Registered: Nov 21 2011
Posts: 5
try67 wrote:
Probably a problem with the field calculation order.
The option "Set Field calculation order" is grayed out, certainly because I only have one field to calculate.

Thanks for this try :)
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
You have to be very careful of the formatting of the date string. Using the "new Date()" requries the date be formatted to comply the ISO format of "yyyy-mm-dd".

Try Date Validation.

George Kaiser

die488
Registered: Nov 21 2011
Posts: 5
THANK YOU
As your example worked, I checked the code to understand the problem, and I found the mistake.

Before : var sDateDemandee = this.getField("Date").value; //the date to evaluate

After : var sDateDemandee = event.value; //the date to evaluate

I don't know why but ... it works pretty well now, even with the date format I use dd/mm/yyyy, for info

Thank you very very much