A Short Acrobat JavaScript FAQ
How do I use date objects?
A
How do I use date objects?
This section discusses the use of
Date
objects within Acrobat. The reader should be
familiar with the JavaScript
Date
object and the
Util
methods that process dates.
JavaScript
Date
objects actually contain both a date and a time. All references to
Date
in
this section refer to the date-time combination.
N
OTE
:
All date manipulations in JavaScript, including those methods that have been
documented in this specification are Year 2000 (Y2K) compliant.
When using the
Date
object, do not use the
getYear()
method, which returns
the current year minus 1900. Instead use the
getFullYear()
method which
always returns a four digit year. For example,
var d = new Date()
d.getFullYear();
N
OTE
:
Converting from a Date to a String
Acrobat provides several date-related methods in addition to the ones provided by the
JavaScript
Date
object. These are the preferred methods of converting between
Date
objects and strings. Because of Acrobat’s ability to handle dates in many formats, the
Date
object does not always handle these conversions correctly.
To convert a
Date
object into a string, the
printd
method of the
Util
object is used.
Unlike the built-in conversion of the
Date
object to a string,
printd
allows an exact
specification of how the date should be formatted.
/* Example of util.printd */
var d = new Date(); // Create a Date object containing the current date
/* Create some strings from the Date object with various formats with
** util.printd */
var s = [ "mm/dd/yy", "yy/m/d", "mmmm dd, yyyy", "dd-mmm-yyyy" ];
for (var i = 0; i < s.length; i++) {
/* print these strings to the console */
console.println("Format " + s[i] + " looks like: "
+ util.printd(s[i], d));
}
The output of this script would look like:
Format
Format
Format
Format
N
OTE
:
mm/dd/yy looks like: 01/15/05
yy/mm/dd looks like: 05/1/15
mmmm dd, yyyy looks like: January 15, 2005
dd-mmm-yyyy looks like: 15-Jan-2005
It is advised that you output dates with a four digit year to avoid ambiguity.
Acrobat JavaScript Scripting Guide
263