Copy link to clipboard
Copied
I am trying to use JS in an adobe form I created. I have a date field in which the user enters the current date. Then, in another field, the days left until the end of the year are displayed. I can do this great in HTML, but behavior in Acrobat isn't quite the same.
Here's what I have so far, but applying it proves difficult.
function DayDiff(StartDate)
{
var TYear=StartDate.getFullYear();
var TDay=new Date("January, 01, 2014");
TDay.getFullYear(TYear);
var DayCount=(TDay-StartDate)/(1000*60*60*24);
DayCount=Math.round(DayCount);
return(DayCount);
}
Copy link to clipboard
Copied
Yes, probably... Change this:
var diffDays = diffMs / oneDay;
To:
var diffDays = Math.floor(diffMs / oneDay);
Copy link to clipboard
Copied
First of all, what's the point of this line:
TDay.getFullYear(TYear);
? It doesn't change anything...
Furthermore, you should use the getTime() method on your Date objects to get the difference between them in ms, after which you can divide it to get the results in days (that you already do).
Copy link to clipboard
Copied
You're right. It didn't anything, so I changed it. However, I'm still unable to have a user enter the date in one input box and have it output the number of days between that input date and the end of the year... How do I make that display happen. It's just not working for me. I have the display field set as "hidden but printable"...does that make any difference?
Copy link to clipboard
Copied
You're close, but not quite there... What are passing to this function, exactly? Where are you converting the string the user inputs to a Date object?
If you want to set year of TDAY to the year following that the user entered, use this: TDay.setFullYear(TYear+1);
Copy link to clipboard
Copied
You're close, but not quite there... What are passing to this function, exactly? Where are you converting the string the user inputs to a Date object?
I guess that's where I'm having trouble. In HTML, the variable for the input text box is the converted string... but in Adobe, I'm not sure how to pass something to this function.
I'm lost! Can you provide more detailed feedback?
Copy link to clipboard
Copied
In Acrobat (also in HTML pages, I believe) you need to explicitly convert a
string into a Date object in order to be able to work with it as such.
There are two ways of doing it: The Date constructor (like you used to
create the other Date object) and an Acrobat-specific method called
util.scand(). The latter is somewhat better than the constructor because
you can specify a date pattern, for example:
util.scand("mm-dd-yyyy", "20-12-2013")
will return a Date object for Dec. 20th, 2013.
Copy link to clipboard
Copied
Okay... so I research the util.scand() and re-wrote the whole thing using that function.
var strStart = this.getField("StartDate").value;
if(strStart.length) {
var dateStart = util.scand("mm/dd/yyyy",strStart);
//Calculation Pemformed
var oneDay = 24 * 60 * 60 * 1000;
var dueMillis = dateStart.getTime() + 5 * oneDay;
var dueDate = new Date(dueMillis);
//Date is printed
event.value = util.printd("dd",dueDate);
}
This is like 99% of what I need to do.
However, I see how to figure out a future date, but how do I write the difference between the start date (StartDate) and the end of the year?
Also, I need to be able to print "Days Left" after the # of days is displayed.
This...
("" + dd + " days left",dueDate);
doesn't work in Acrobat...
Any ideas?
Copy link to clipboard
Copied
You were on the right path before to calcluate the number of days to the end of the year...
This will give you the first day of the next year from dateStart:
var firstDayNextYear = util.scand("dd/mm/yyyy", "01/01/" + dateStart.getFullYear()+1);
Now you want to calculate the difference between them, right?
var diffMs = firstDayNextYear.getTime() - dateStart.getTime();
var diffDays = diffMs / oneDay;
And to apply it to the field where this is the calculation script you simply use:
event.value = diffDays + " days left";
Copy link to clipboard
Copied
Here's the entire thing, the way we've worked it out so far... But it's not rendering the amount of days between the StartDate and the end of the year... Not sure where I went wrong... Ideas?
//Start 'Days Until' Script
var strStart = this.getField("StartDate").value;
if(strStart.length) {
var dateStart = util.scand("mm/dd/yyyy",strStart);
//Calculation Pemformed
var oneDay = 24 * 60 * 60 * 1000;
var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" +
dateStart.getFullYear()+1);
var diffMs = firstDayNextYear.getTime() - dateStart.getTime();
var diffDays = diffMs /
oneDay;
//Date is printed
event.value = diffDays + " days left";
}
Thanks!
Copy link to clipboard
Copied
Are there any error messages in the console? Does it produce any output at all?
Copy link to clipboard
Copied
No... the output box doesn't produce anything. And it did before... I'm not sure where to go from here...
Copy link to clipboard
Copied
Start debugging... Add console.println() commands to check the values of the various variables and make sure that they are what you expect them to be...
Also, you might want to use valueAsString in your first line instead of value.
Copy link to clipboard
Copied
I started debugging and this is where I'm at right now...
getField("DATE") is null
1:Field:Calculate
TypeError: getField("DATE") is null
1:Field:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
I'm not sure I really understand this too well...
Copy link to clipboard
Copied
There's not field in your file called "DATE"... Remember that JS is
case-sensitive, so if your field is actually called "date", you have to
write it that way.
Copy link to clipboard
Copied
Okay... I made an adjustment and now I'm getting some output at least... Here's the code...
//Start 'Days Until' Script
var strStart = this.getField("StartDate").value;
if(strStart.length) {
var dateStart = util.scand("mm/dd/yyyy",strStart);
//Calculation Pemformed
var oneDay = 24 * 60 * 60 * 1000;
var firstDayNextYear = util.scand("mm/dd/yyyy", dateStart.setFullYear()+1);
var diffMs = firstDayNextYear.getTime() - dateStart.getTime();
var diffDays = diffMs /
oneDay;
//Date is printed
event.value = diffDays + " days left";
}
Now, it's displaying NaN days left in the output field.
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
firstDayNextYear is null
13:Field:Calculate
TypeError: firstDayNextYear is null
13:Field:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
getField("DATE") is null
1:AcroForm:PTO:Calculate
TypeError: getField("DATE") is null
1:AcroForm:PTO:Calculate
In the form PTO, I had the following:
DATE(YEAR(StartDate),12,31)-StartDate
I have lower-cased the "date" word and completely removed this line. The error still doesn't go away when I re-debug it. Any ideas?
And what do you think this line means?
13:Field:Calculate
TypeError: firstDayNextYear is null
I think we're getting somewhere, but not sure how to fix some of these things.
Copy link to clipboard
Copied
You messed up the definition of firstDayNextYear. Go back to my code and
see how it should be done.
Copy link to clipboard
Copied
Here is my code up to this point. You'll notice that down below, I have re-compiled the code and
the debugger is showing significantly less errors.
//Start 'Days Until' Script
var strStart = this.getField("StartDate").value;
if(strStart.length) {
var dateStart = util.scand("mm/dd/yyyy",strStart);
//Calculation Pemformed
var oneDay = 24 * 60 * 60 * 1000;
var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" + dateStart.getFullYear()+1);
var diffMs = firstDayNextYear.getTime() - dateStart.getTime();
var diffDays = diffMs /
oneDay;
//Date is printed
event.value = diffDays + " days left";
}
These "null" errors... I am not sure where to go with those.
And there's a "date is not defined" error, too.
firstDayNextYear is null
12:Field:Calculate
TypeError: firstDayNextYear is null
12:Field:Calculate
date is not defined
1:Field:Calculate
ReferenceError: date is not defined
1:Field:Calculate
Any ideas here?
Copy link to clipboard
Copied
I re-wrote the code for you, and added some debugging commands:
event.value = "";
var strStart = this.getField("StartDate").valueAsString;
if(strStart.length) {
console.println("strStart:"+strStart);
var dateStart = util.scand("mm/dd/yyyy",strStart);
console.println("dateStart:"+dateStart);
var oneDay = 24 * 60 * 60 * 1000;
var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" + (dateStart.getFullYear()+1));
console.println("firstDayNextYear:"+firstDayNextYear);
var diffMs = firstDayNextYear.getTime() - dateStart.getTime();
var diffDays = diffMs / oneDay;
console.println("diffDays:"+diffDays);
event.value = diffDays + " days left";
}
Copy link to clipboard
Copied
Okay try67... We're really close now...
Here's the debugging info. The decimal place goes out about 14 places, but it's behaving properly.
firstDayNextYear is null
12:Field:Calculate
TypeError: firstDayNextYear is null
12:Field:Calculate
date is not defined
1:Field:Calculate
ReferenceError: date is not defined
1:Field:CalculatestrStart:03/01/2013
dateStart:Fri Mar 01 2013 00:00:00 GMT-0700 (US Mountain Standard Time)
firstDayNextYear:Wed Jan 01 2014 00:00:00 GMT-0700 (US Mountain Standard Time)
diffDays:306.00000001157406
date is not defined
1:AcroForm:PTO:Calculate
ReferenceError: date is not defined
1:AcroForm:PTO:CalculatestrStart:04/05/2013
dateStart:Fri Apr 05 2013 00:00:00 GMT-0700 (US Mountain Standard Time)
firstDayNextYear:Wed Jan 01 2014 00:00:00 GMT-0700 (US Mountain Standard Time)
diffDays:271.00000003472223
date is not defined
1:AcroForm:PTO:Calculate
ReferenceError: date is not defined
1:AcroForm:PTO:Calculate
I'm going to see what I can do now about the decimal places...
Copy link to clipboard
Copied
Okay... So, I played around with the User Input part of the form a bit and put in different dates. If I put in a date where the days remaining in the year is a whole number, then it shows like 335 Days Left (I used 01/31/2013). But, when I put in tomorrow's date (02/01/2013), I get 334.00000001157406.
I think it might be something in the milliseconds calculation. What are your thoughts?
Copy link to clipboard
Copied
Yes, probably... Change this:
var diffDays = diffMs / oneDay;
To:
var diffDays = Math.floor(diffMs / oneDay);
Copy link to clipboard
Copied
That worked. This is great!
Thanks for all your help with this. You've certainly taught me quite a bit about js in Acrobat. You're really good at this!

