Skip to main content
Participant
August 4, 2022
Question

hide field according dates in Adobe JavaScript

  • August 4, 2022
  • 2 replies
  • 1303 views

Hello everyone,

I'm trying to create a PDF form where I'd like to hide 4 checkboxes if the date in the "Date From" field is
between 04/02/2023 and 05/03/2023.
If the "Date from" field is less than or greater than these 2 dates, then the checkboxes are visible.

Below what I started but it doesn't work at all..

Any ideas?

Thanks very much.

var date1 = "05/02/2023"
var date2 = "04/03/2023"
var date = this.getField("Date Du").valueAsString

if(date >= date1 && date >= date2) {

this.getField("LP 1h30 1").hidden = true;
this.getField("LP 1h30 2").hidden = true;
this.getField("LP 1h30 3").hidden = true;
this.getField("LP 1h30 4").hidden = true;

}
else {
this.getField("LP 1h30 1").hidden = false;
this.getField("LP 1h30 2").hidden = false;
this.getField("LP 1h30 3").hidden = false;
this.getField("LP 1h30 4").hidden = false;
}

 

2 replies

Nesa Nurani
Community Expert
Community Expert
August 4, 2022

What should happen if date field is empty?

You need to convert dates to date object to compare them.

To show/hide fields, use 'display' property, display.visible/display.hidden.

I assume those dates are in format dd/mm/yyyy?

You used >= for both condition in your script, second one should be <= also you said field name is "Date from" and in script you use "Date Du", as 'Validation' script of that field use like this:

 

var date1 = util.scand("dd/mm/yyyy","05/02/2023");
var date2 = util.scand("dd/mm/yyyy","04/03/2023");
var date =  util.scand("dd/mm/yyyy",event.value);

if(date.getTime() >= date1.getTime() && date.getTime() <= date2.getTime()) {

this.getField("LP 1h30 1").display = display.hidden;
this.getField("LP 1h30 2").display = display.hidden;
this.getField("LP 1h30 3").display = display.hidden;
this.getField("LP 1h30 4").display = display.hidden;

}
else {
this.getField("LP 1h30 1").display = display.visible;
this.getField("LP 1h30 2").display = display.visible;
this.getField("LP 1h30 3").display = display.visible;
this.getField("LP 1h30 4").display = display.visible;
}

 

 

 

 

 

 

try67
Community Expert
Community Expert
August 4, 2022

It's not a good idea to compare Date objects directly. You should use their getTime method for that.

Here's an example of what can happen if you don't do that:

 

var date1 = util.scand("dd/mm/yyyy","05/02/2023");
var date2 = util.scand("dd/mm/yyyy","05/02/2023");
console.println("Direct compare: " + date1==date2);
console.println("Time compare: " + date1.getTime()==date2.getTime());
Nesa Nurani
Community Expert
Community Expert
August 4, 2022

Yes, you are correct, script fixed, thanks. Although it may still give incorrect result because of timezones, it would be more precise to add time to the date format as well.

Bernd Alheit
Community Expert
Community Expert
August 4, 2022