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

Compare 2 dates and create if/then satement

bspongberg
Registered: Sep 7 2011
Posts: 3
Answered

Hi everyone. I've browsed these forums for months and found a lot of great answers to every question I've had so far... except one. And I've searched this and other forums, Google and everywhere else I could think of and I still have not found the answer.
 
I'm working on an order form with various quantities, pricing and other charges involved. There is one field that will not work no matter what I do. It is a discount field that should be applied based on whether or not the current date is past a specified cut-off date.
 
First, I have two date fields. One ("ModDate") is calculated as the modification date of the document with this code in the custom calculation script:
 
event.value = util.printd("dddd mmmm d, yyyy h:MM tt", info.ModDate);
 
The second date field ("DiscountEnds") is the cut-off date for the discount and is entered manually.
 
And finally I have a field that should be calculating either a 25% discount if the current ModDate is before the deadline or o% discount if the current ModDate is after the deadline.
 
The most recent JavaScript I used was this:
 
event.value = 0; // Initial discount value
 
var date1 = this.getField("ModDate").value;
var date2 = this.getField("DiscountEnds").value;
var disc = this.getField("EqTotal").value;
 
if(date1 < date2)
event.value = disc * (-.25);
  
Prior to that, I had some limited success with this script:
 
if (this.getField("ModDate").value > this.getField("DiscountEnds").value) event.value = this.getField("EqTotal").value * (-.25);
else event.value = 0;
 
This script actually calculated the discount, but it was random when it would apply it. It was not based on a comparison of the two dates as far as I could tell.
 
I'm wondering if I need to convert the two dates to a time or a number for comparison... but I can't figure out how to do that.
 
Note: Using Acrobat Pro 10.1.0 on OS X
 
Thanks in advance!

My Product Information:
Acrobat Pro 10.1, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
If you're thinking that info.ModDate returns the current date/time, that's not necessarily the case. If you want to tell if the date represented by the value of the DiscountEnds field is in the past, you'd do something like:

  1. // Get the current date and set to midnight
  2. var date1 = (new Date()).setHours(0, 0, 0, 0);
  3.  
  4. // Convert the value of DiscountEnds to a date
  5. var date2 = util.scand("mm/dd/yyyy", getField("DiscountEnds").value);
  6.  
  7. // Get the total
  8. var total = getField("EqTotal").value;
  9.  
  10. // Calculate the discount
  11. event.value = date1 <= date2 ? -.25 * total : 0;
Adjust the "mm/dd/yyyy" used with util.scand to match the format of the value of the DiscountEnds field. This is all dealing with the local time, so if the time zone matters, you'd have to take that into account. Also, this could of course be circumvented by the user changing their system date.
bspongberg
Registered: Sep 7 2011
Posts: 3
That did exactly what I needed it to. Thank you very much.

The only thing I can't really tell is whether using the (new Date()).setHours(0, 0, 0, 0) will change the calculation day by day, or if it will remain locked to the correct calculation once the form is signed and the fields are locked.

Also, thank you for pointing out the time zone differences, but fortunately for me, the deadline is not that specific, so as long as it's sent by midnight of whatever time zone the client is in, we'll accept it with the discount applied.

-Bill
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
If that script is in a calculate event, it will update when the value if any field on the form is changed, including any signature fields. You can change the location of the script so that it is only triggered when the EqTotal value changes, or on some other condition. The last two lines of the script would have to be changed if you do this.
bspongberg
Registered: Sep 7 2011
Posts: 3
That's exactly what I needed. Once the user fills the form out, they sign the form and it locks the form from further changes. If the script only recalculates on field changes, that will do the trick. As long as a button state doesn't count as a field change... which as I understand is not the case.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
That's right. A button does not have a value.

Even though you don't need to worry about it in this case, it is possible to set up a calculation script so that it only updates the value of the field to which it is attached if it was triggered due to a change in one or more specific fields. In this case, the script could check the value of event.source, and if it is equal to the EqTotal field, go ahead with the update. Otherwise don't.