Skip to main content
Mae_RVT
Inspiring
October 31, 2019
Answered

Change Text Color base on date

  • October 31, 2019
  • 2 replies
  • 1212 views

Hi,

 

I am working on a PDF in Acrobat Pro DC and need some help with some JS that is eluding me. I am trying to make the text color change based on an expiration date. If the date is today or earlier to change text to red. otherwise black.

 

Here is what i have at the moment put in the custom calculation script, i have tried many different versions i just cant get it to work. I cant seem to find anything anywhere on line for this.

 

Any assistance is greatful on where i could be going wrong.

 

var Date = util.printd("dd-mmm-yyyy", new Date());

if (event.value.date <= Date)
{
    event.target.textColor = color.red;
}
else
{
    event.target.textColor = color.black;
}
This topic has been closed for replies.
Correct answer try67

A couple of issues there:

1. Don't name a variable "Date". There's a built-in object with that name and you'll get conflicts between them.

2. There's no such thing as "event.value.date". If by that you mean that you want to use the value the user entered then you need to access event.value, convert it to a Date object (using util.scand) and then you'll be able to compare it to another Date object.

2 replies

JR Boulay
Community Expert
Community Expert
October 31, 2019
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 31, 2019

A couple of issues there:

1. Don't name a variable "Date". There's a built-in object with that name and you'll get conflicts between them.

2. There's no such thing as "event.value.date". If by that you mean that you want to use the value the user entered then you need to access event.value, convert it to a Date object (using util.scand) and then you'll be able to compare it to another Date object.

Mae_RVT
Mae_RVTAuthor
Inspiring
November 1, 2019

Hi,

 

Thanks, the util.scand is what i was after. util.printd was not quite as it was converting to a string from a date which messed it up. I changed the variable name as you suggested also.

 

This is the final working code 🙂

var DT = new Date();
var EXP = this.getField("test expiry").value;
var EXD = util.scand("dd-mmm-yyyy", EXP);

if (EXD.valueOf() <= DT.valueOf())
{
    event.target.textColor = color.red;
}
else
{
    event.target.textColor = color.black;
}
this.calculateNow();