• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change Text Color base on date

Explorer ,
Oct 30, 2019 Oct 30, 2019

Copy link to clipboard

Copied

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;
}
TOPICS
Acrobat SDK and JavaScript

Views

661

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
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 31, 2019 Oct 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.

Votes

Translate

Translate
Community Expert ,
Oct 31, 2019 Oct 31, 2019

Copy link to clipboard

Copied

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.

Votes

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
community guidelines
Explorer ,
Oct 31, 2019 Oct 31, 2019

Copy link to clipboard

Copied

LATEST

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();

 

Votes

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
community guidelines
Community Expert ,
Oct 31, 2019 Oct 31, 2019

Copy link to clipboard

Copied

Votes

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
community guidelines