Forum Moderators: open

Message Too Old, No Replies

Date subtraction in Acrobat

         

lenpartico

6:03 am on Dec 28, 2004 (gmt 0)

10+ Year Member




Hi,
I am trying to do a date subtraction script in Acrobat "custom calculation script", I am not versed in Javascript, but this is what I have:
One field is Date1 and the other field is Date2 and the answer field is Period. I will enter a date in Date 1 and another date in Date2 and Period will give the period of time between the two dates in weeks.

var date1 = this.getField("Date1").value;
var date2 = this.getField("Date2").value;
function period(date1, date2)
{
var period = (date1-date2);

return period/1000/3600/168;
}

It don't seem to work, any help will be appreciated.
Thanks

orion_rus

11:26 am on Dec 28, 2004 (gmt 0)

10+ Year Member



I think u need to convert it to a integer type with
parseInt(string,10)
good luck to you

lenpartico

1:54 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I am not versed in Javascript, could someone modify the script for me.
Thanks.

adni18

8:46 pm on Dec 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var date1 = this.getField("Date1").value;
var date2 = this.getField("Date2").value;
function period(date1, date2)
{
date1=parseInt(date1, 10);
date2=parseInt(date2, 10);
var period = (date1-date2);

return period/1000/3600/168;
}

lenpartico

12:00 am on Dec 29, 2004 (gmt 0)

10+ Year Member



Thanks Adni18,
I appreciate your help but I am still having aproblem:-

Acrobat JavaScript Debugger Functions Version 7.0
Acrobat EScript Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Wizard Functions Version 7.0
Acrobat Multimedia Version 7.0
Acrobat SOAP 7.0
TypeError: this.getField("Date1") has no properties
1:Field:Calculate

Your help will beappreciated.
Thanks again.

adni18

2:00 am on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the script is in a <script> tag, "this" cannot be accessed in most browsers. Moreover, the getField() is necessary in order to debug. Please post from the part of your script that says

function getField(...)
{

to

}

lenpartico

4:11 am on Dec 29, 2004 (gmt 0)

10+ Year Member



What I am doing is making a little form in Acrobat Professional 7, and I am trying to get a period of time from two dates that I input; one field is Date1 and the other field is Date2 and the answer field is Period, so I have three fields. I will enter a date in Date 1 and another date in Date2 and Period will give the period of time between the two dates in weeks. To do this I have to insert a script using "custom calculation script" which is built into Acrobat Professional. I am not versed in Javascript, but what I have is what you modified for me, that is all the code. When I insert the code the built-in debugger gives the message "Acrobat JavaScript Debugger Functions Version 7.0
Acrobat EScript Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Wizard Functions Version 7.0
Acrobat Multimedia Version 7.0
Acrobat SOAP 7.0
TypeError: this.getField("Date1") has no properties
1:Field:Calculate"
Apparently "Calculate" is a keyword that somehow has to be inserted. This is all done in Acrobat Pro.
I do appreciate your willingness to help.
Thanks again.

adni18

4:21 am on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this. just make sure you define the elements with something like id="Date1" instead of name="Date1":

function getField(prime)
{
return document.getElementById(prime);
}
var date1 = getField("Date1").value;
var date2 = getField("Date2").value;
function period(a, b)
{
a=parseInt(date1, 10);
b=parseInt(date2, 10);
var period = (a-b);
return period/1000/3600/168;
}
getField("period").value=period(date1, date2);

lenpartico

4:38 am on Dec 29, 2004 (gmt 0)

10+ Year Member



Copied and pasted your last modification and got:

TypeError: redeclaration of function getField
1:Field:Calculate

I really appreciate your help but I hope I am not being a pain.
Thanks again.

adni18

4:17 pm on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess that means we need to use a different function name:

function findField(prime)
{
return document.getElementById(prime);
}
var date1 = findField("Date1").value;
var date2 = findField("Date2").value;
function period(a, b)
{
a=parseInt(date1, 10);
b=parseInt(date2, 10);
var period = (a-b);
return period/1000/3600/168;
}
findField("period").value=period(date1, date2);

RonPK

4:28 pm on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lenpartico, what does the input look like?

I'm asking because whenever I need to do date calculations, I use three fields to compose the date: day, month and year. I then create a date object, in order to convert the input to a timestamp, which makes calculations possible.

lenpartico

2:21 am on Dec 30, 2004 (gmt 0)

10+ Year Member



It is just three fields and I am trying to get a period of time from two dates that I input; one field is Date1 and the other field is Date2 and the answer field is Period, so I have three fields.
I will enter a date in Date 1 and another date in Date2 and Period will give the period of time between the two dates in weeks.
To do this I have to insert a script using "custom calculation script" which is built into Acrobat Professional.
I can email you the document it is 64k in size.

lenpartico

2:23 am on Dec 30, 2004 (gmt 0)

10+ Year Member



I input the new data and this is the error
Acrobat SOAP 7.0
ReferenceError: document is not defined
3:Field:Calculate
Is it possible to email you the document? It is 64k in size.

lenpartico

2:40 am on Dec 30, 2004 (gmt 0)

10+ Year Member



See if this helps, it was taken from Acrobat JavaScript Scripting Guide - AcroJSGuide.pdf
Calculation Options
Calculation options make it possible to automate mathematical calculations associated
with form fields. To apply a calculation to a form field action, invoke the form field’s
setAction method, pass "Calculate" as the first parameter, and pass a script
containing a call to a calculation script as the second parameter.
If you would like to perform simple calculations on an array of form field values, you may
use a convenient script already written for you called AFSimple_Calculate in the
second parameter. The function AFSimple_Calculate is located in
Javascripts\aform.js, and requires 2 parameters: cFunction (the type of calculation
to be performed) and cFields (a field list that may be separated by commas or spaces, or
may be an array). The first parameter specifies the type of calculation, which may be a sum
("SUM"), product ("PRD"), average ("AVG"), minimum ("MIN"), or maximum ("MAX").
For example, the following code calculates the sum of the values entered into two text
areas on a form:
var arr = new Array("line.1", "line.2");
f.setAction(
"Calculate",
‘AFSimple_Calculate("SUM",arr)’
);

RonPK

11:49 am on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm getting the impression that you're expecting JavaScript to be smart enough to do calculations on input like 'Jan 1, 2004' and '12-1-67'. That is unlikely to do what you want. Dates are rather complex things to do calculations with. Here's a short example of how I'd handle this in ordinary HTML and JavaScript:

<script type="text/javascript"> 
function showDiff(f) {
var date1 = new Date(f.d1y.value, f.d1m.value - 1, f.d1d.value);
var date2 = new Date(f.d2y.value, f.d2m.value - 1, f.d2d.value);
f.diff.value = Math.floor((date2.getTime() - date1.getTime()) / 604800000 );
}
</script>

<form>
Date 1: day<input type="text" name="d1d" size="2">
month<input type="text" name="d1m" size="2">
year<input type="text" name="d1y" size="4"><br>
Date 2: day<input type="text" name="d2d" size="2">
month<input type="text" name="d2m" size="2">
year<input type="text" name="d2y" size="4"><br>
<input type="button" value="show difference" onclick="showDiff(this.form)"><br>
<br>
Difference, in weeks: <input type="text" name="diff" size="4">
</form>

The function showDiff() creates two date objects using the input in the form fields:
var date2 = new Date(year, month, day);
For some reason January is considered to be month #0, so we need to substract 1 from the user's input.
The method date.getTime() returns the number of milliseconds between midnight, 1/1/1970 (GMT) and the date: an integer, on which we can finally perform calculations.
The method Math.floor(x) rounds the value of x down; maybe you prefer Math.round or Math.ceil.

Please note that all necessary input validation was omitted from the script.

HTH

lenpartico

3:05 pm on Dec 30, 2004 (gmt 0)

10+ Year Member




"I'm getting the impression that you're expecting JavaScript to be smart enough to do calculations on input like 'Jan 1, 2004' and '12-1-67'. That is unlikely to do what you want."

Yes, that is what I am hoping for, a time difference in weeks from dates that are inserted into an Acrobat form.

Thanks for your efforts.

I guess that I will just have to do it manually.

Thanks again.

adni18

3:29 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope it works out for you.

lenpartico

12:11 am on Dec 31, 2004 (gmt 0)

10+ Year Member



Got this from page 255 0f
Technical Note # 5186
Acrobat JavaScript Object Specification
Version 5.1

Date arithmetic
It is often useful to do arithmetic on dates to determine things like the time interval between
two dates or what the date will be several days or weeks in the future. The JavaScript Date
object provides several ways to do this. The simplest and possibly most easily understood
method is by manipulating dates in terms of their numeric representation. Internally, JavaScript
dates are stored as the number of milliseconds (one thousand milliseconds is one whole
second) since a fixed date and time. This number can be retrieved through the valueOf method
of the Date object. The Date constructor allows the construction of a new date from this
number.

/* Example of date arithmetic. */
/* Create a Date object with a definite date. */
var d1 = util.scand("mm/dd/yy", "4/11/76");
/* Create a date object containing the current date. */
var d2 = new Date();
/* Number of seconds difference. */
var diff = (d2.valueOf() - d1.valueOf()) / 1000;
/* Print some interesting stuff to the console. */
console.println("It has been " + diff + " seconds since 4/11/1976");
console.println("It has been " + diff / 60 + " minutes since 4/11/1976");
console.println("It has been " + (diff / 60) / 60 + " hours since 4/11/1976");
console.println("It has been " +
((diff / 60) / 60) / 24 + " days since 4/11/1976");
console.println("It has been " +
(((diff / 60) / 60) / 24) / 365 + " years since 4/11/1976");
The output of this script would look something like:
It has been 718329600 seconds since 4/11/1976
It has been 11972160 minutes since 4/11/1976
It has been 199536 hours since 4/11/1976
It has been 8314 days since 4/11/1976
It has been 22.7780821917808 years since 4/11/1976

Tried this

/* Example of date arithmetic. */
/* Create a Date object with a definite date. */
var date1 = util.scand("mm/dd/yy", "4/11/76");
/* Create a date object containing the current date. */
var date2 = new Date();
/* Number of days difference. */
var diff = (date2.valueOf() - date1.valueOf()) / (1000*3600*168);
/* Print some interesting stuff to the console. */

console.println("It has been " + diff + " weeks since 4/11/1976");

but no string is printed. Any help?

RonPK

1:38 am on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see any errors. Doesn't Acrobat have some kind of error reporting built in? Otherwise, try to debug by printing the value created on the previous line and see where script execution stops.

lenpartico

2:02 am on Dec 31, 2004 (gmt 0)

10+ Year Member




Acrobat has a built-in debugger but it compiles without a problem but there is no return value.

I don't know how to try to debug by printing the value created on the previous line.

lenpartico

4:34 am on Dec 31, 2004 (gmt 0)

10+ Year Member



Got it now!
This is what I needed:

var date1=AFParseDateEx(getField("date1").value, "mdy");
var date2=AFParseDateEx(getField("date2").value, "mdy");
var diff=date2-date1;
event.value=diff/86400000;

Thanks for all your help and efforts.

lenpartico

4:39 am on Dec 31, 2004 (gmt 0)

10+ Year Member



Now, I have four Fields Date1, Date2, Weeks and Days; so period is now broken into Weeks and Days, could you modify it for me to get Weeks and Days?
Thanks.

lenpartico

5:14 am on Dec 31, 2004 (gmt 0)

10+ Year Member



var date1=AFParseDateEx(getField("date1").value, "mdy");
var date2=AFParseDateEx(getField("date2").value, "mdy");
var diff=date2-date1;
event.value=diff/86400000*7;
would give weeks
so the mod would give weeks and the rem will give days,
but I do not know how to code that. Please help.

lenpartico

5:15 am on Dec 31, 2004 (gmt 0)

10+ Year Member



var date1=AFParseDateEx(getField("date1").value, "mdy");
var date2=AFParseDateEx(getField("date2").value, "mdy");
var diff=date2-date1;
event.value=diff/86400000/7;
would give weeks
so the mod would give weeks and the rem will give days,
but I do not know how to code that. Please help.

RonPK

10:52 am on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Suppose that the start value of weeks (your event.value) is 7.2345
Math.floor(7.2345) returns 7 (weeks rounded down)
7.2345 - Math.floor(7.2345) returns 0.2345
Math.floor(0.2345*7) returns 1 (days rounded down)

HTH...

lenpartico

1:47 am on Jan 1, 2005 (gmt 0)

10+ Year Member



Thanks, but this don't seem to work.Math.floor(7.5 or greater) returns 8.
I need to get the quotient and remainder functions.
Thanks again.

lenpartico

2:27 am on Jan 1, 2005 (gmt 0)

10+ Year Member



This is what I did

var Today=AFParseDateEx(getField("Today").value, "mdy");
var LMP=AFParseDateEx(getField("LMP").value, "mdy");
var diff=Today-LMP;
event.value = (diff/86400000/7);
Math.floor(event.value);

Instead of returning the largest whole number less than or equal to the specified number it gives the closest whole number(if the part following the decimal is 5 or greater it gives the next whole number).

RonPK

12:24 pm on Jan 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is impossible.

Please check whether the script does what you think it does:

var Today=AFParseDateEx(getField("Today").value, "mdy");
var LMP=AFParseDateEx(getField("LMP").value, "mdy");
var diff=Today-LMP;
console.println("diff = " + diff);
event.value = (diff/86400000/7);
console.println("event.value = " + event.value);
var floored = Math.floor(event.value);
console.println("floored = " + floored);

lenpartico

4:01 pm on Jan 1, 2005 (gmt 0)

10+ Year Member



This works:

var Today=AFParseDateEx(getField("Today").value, "mdy");
var LMP=AFParseDateEx(getField("LMP").value, "mdy");
var diff=Today-LMP;
event.value=Math.floor(diff/86400000/7)
document.write(event.value);

Thanks.

lenpartico

6:50 pm on Jan 1, 2005 (gmt 0)

10+ Year Member



How about adding weeks -
It is just three fields, and I am trying to get a new date, the first field I input today’s date, the second one I also input the required number of weeks that I need, and the answer field gives the new date.

This is what I have:

var today=AFParseDateEx(getField("Today").value, "mdy");
var add=getField("AddWeeks").value*1000 * 60 * 60 * 24 * 7;
var newdate = today+add;

/* set the event value to the calculated date */
this.getField("NewDate").value = util.printd("mdy", newdate) ;

There seems to be a problem with it.

This 35 message thread spans 2 pages: 35