Date Comparison Script

Community Beginner ,
Feb 19, 2019

Copy link to clipboard

Copied

I have multiple date fields, ie:

     Date.0

     Date.1

     …

     Date.5

     Date.6

These are formatted mm/dd/yyyy

In the custom validation script, after Date.0, I use the following to get the current number of the date field:

     var str = event.target.name;

     var res = str.charAt(str.length-1);

I then use this.getField("0." + (res-1)).value to compare against event.value.

What want to do is:

  1. Compare the two dates to determine if the date just entered pre-dates the preceding date.
  2. And, if it does, serve up:  app.alert("The date you entered is invalid. Please enter a date that does not pre-date the previous entry date." ,0);

I would very much appreciate help in getting a working script.

Thank you

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Date comparisons in JavaScript are easy, as long as you can correctly parse the date.  If the format you've specified is consistent, then it's even easier.

Here's an example:

var strDate1 = "2/11/2019";

var strDate2 = "2/12/2019";

// Create the actual date objects

var date1 = new Date(strDate1);

var date2 = new Date(strDate2);

// Do the comparison

if(date2 > date1)

  app.alert("Date 2 is later than date 1");

Very simple

TOPICS
Acrobat SDK and JavaScript, Windows

Views

146

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Date Comparison Script

Community Beginner ,
Feb 19, 2019

Copy link to clipboard

Copied

I have multiple date fields, ie:

     Date.0

     Date.1

     …

     Date.5

     Date.6

These are formatted mm/dd/yyyy

In the custom validation script, after Date.0, I use the following to get the current number of the date field:

     var str = event.target.name;

     var res = str.charAt(str.length-1);

I then use this.getField("0." + (res-1)).value to compare against event.value.

What want to do is:

  1. Compare the two dates to determine if the date just entered pre-dates the preceding date.
  2. And, if it does, serve up:  app.alert("The date you entered is invalid. Please enter a date that does not pre-date the previous entry date." ,0);

I would very much appreciate help in getting a working script.

Thank you

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Date comparisons in JavaScript are easy, as long as you can correctly parse the date.  If the format you've specified is consistent, then it's even easier.

Here's an example:

var strDate1 = "2/11/2019";

var strDate2 = "2/12/2019";

// Create the actual date objects

var date1 = new Date(strDate1);

var date2 = new Date(strDate2);

// Do the comparison

if(date2 > date1)

  app.alert("Date 2 is later than date 1");

Very simple

TOPICS
Acrobat SDK and JavaScript, Windows

Views

147

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2019 0
Adobe Community Professional ,
Feb 19, 2019

Copy link to clipboard

Copied

Date comparisons in JavaScript are easy, as long as you can correctly parse the date.  If the format you've specified is consistent, then it's even easier.

Here's an example:

var strDate1 = "2/11/2019";

var strDate2 = "2/12/2019";

// Create the actual date objects

var date1 = new Date(strDate1);

var date2 = new Date(strDate2);

// Do the comparison

if(date2 > date1)

  app.alert("Date 2 is later than date 1");

Very simple

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 19, 2019 1
Community Beginner ,
Feb 19, 2019

Copy link to clipboard

Copied

THANK YOU!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 19, 2019 0
Community Beginner ,
Feb 19, 2019

Copy link to clipboard

Copied

Hmmmmmm......seems something is still wrong.

var str = event.target.name;

var res = str.charAt(str.length-1);

var CDate = this.getField("0." + res).value;

var LDate = < this.getField("0." + (res-1)).value;

var date1 = new Date(CDate);

var date2 = new Date(LDate);

if (date1 < date2) {

  1. app.alert("This date is before the last date." ,0);

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 19, 2019 0
Adobe Community Professional ,
Feb 19, 2019

Copy link to clipboard

Copied

There are 2 errors in the "getField" code. 

1)  You said the names of your date fields are "Date.0" Date.1" etc. The code does not get these fields.

2)  As pointed out by the debugger, there is a syntax error at line 5.  This is the "<" symbol right after "LDate =".  Always look over your code for these simple errors. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 19, 2019 1