A
A Short Acrobat JavaScript FAQ
How do I use date objects?
Converting from a string to a date
To convert a string into a
Date
object, use the
Util
object’s
scand
method. It accepts a
format string that it uses as a hint as to the order of the year, month, and day in the input
string.
/* Example of util.scand */
/* Create some strings containing the same date in differing formats. */
var s1 = "03/12/97";
var s2 = "80/06/01";
var s3 = "December 6, 1948";
var s4 = "Saturday 04/11/76";
var s5 = "Tue. 02/01/30";
var s6 = "Friday, Jan. the 15th, 1999";
/* Convert the strings into Date objects using util.scand */
var d1 = util.scand("mm/dd/yy", s1);
var d2 = util.scand("yy/mm/dd", s2);
var d3 = util.scand("mmmm dd, yyyy", s3);
var d4 = util.scand("mm/dd/yy", s4);
var d5 = util.scand("yy/mm/dd", s5);
var d6 = util.scand("mmmm dd, yyyy", s6);
/* Print the dates to the console using util.printd */
console.println(util.printd("mm/dd/yyyy", d1));
console.println(util.printd("mm/dd/yyyy", d2));
console.println(util.printd("mm/dd/yyyy", d3));
console.println(util.printd("mm/dd/yyyy", d4));
console.println(util.printd("mm/dd/yyyy", d5));
console.println(util.printd("mm/dd/yyyy", d6));
The output of this script would look like:
03/12/1997
06/01/1980
12/06/1948
04/11/1976
01/30/2002
01/15/1999
Unlike the date constructor (
new Date(...)
),
scand
is rather forgiving in terms of the
string passed to it.
N
OTE
:
Given a two digit year for input,
scand
resolves the ambiguity as follows: if the year
is less than 50 then it is assumed to be in the 21st century (i.e. add 2000), if it is
greater than or equal to 50 then it is in the 20th century (add 1900). This heuristic is
often known as the
Date Horizon.
264
Acrobat JavaScript Scripting Guide