A Short Acrobat JavaScript FAQ
How do I use date objects?
A
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
It
It
It
It
has
has
has
has
has
been
been
been
been
been
718329600 seconds since 4/11/1976
11972160 minutes since 4/11/1976
199536 hours since 4/11/1976
8314 days since 4/11/1976
22.7780821917808 years since 4/11/1976
Acrobat JavaScript Scripting Guide
265