Tutorials, tools, scripts and samples for scripting Acrobat and PDFpdfscripting.com
HomeVideo TutorialsCopy-n-Paste ScriptsDownload LibraryFree ContentSearchMEMBERS LOG-IN
Home | Date and Time Scripts

Date and Time Handling - Information and Scripts (Script pages at bottom)

Thom Parker


Dates and times are used in a large variety of ways across all manner of electronic documents. But for all this variety there are only a few things you really need to know to effectively handle dates and times. The kinds of operations performed with dates and times can be divided into three broad categories- parsing date text (handling input data), date manipulation (extracting information and/or doing calculations), and formatting date text (handling output data).

The Date Object:

Core JavaScript provides a general purpose object for representing both Dates and Times, the Date Object. This is a well defined object that provides a rich set of functionality for parsing date strings and specific information from a date/time, including year, month, day, weekday, and timezone data. This object will be used in most date/time operations.

The Date Object represents a specific date with the number of milliseconds from midnight on December 31st, 1969, i.e, the first millisecond of 1970 in Greenwich England, which is time zone zero, also called UTC or Zulu time.

Acrobat JavaScript provides additional functions that fill in the holes for things the Date object does not do well. Specifically, converting Date/Time strings into Date Objects and formatting Date Objects into Date/Time strings.

Date/Time Input and Parsing:

To enter a date or time into a form the user will usually use one of three methods- manual text entry, selection from a drop down list, or selection from a popup calendar. Whatever the method, the value entered into the field is a text representation of the date and/or time, a date/time string.

To use this date/time in a script it has to be converted into something usable for JavaScript code. This usually means converting the date/time text into a Date Object. The Date object itself provides some very basic operations for this conversion, but these are only marginally useful. Acrobat JavaScript provides a much more powerful function for converting dates, the "util.scand()" function. This function uses the same formatting strings shown in the table below in the section on Date/Time Formatting.

For values that represent time intervals (such as the number of days), or are purely time (such as "3:00pm") and do not contain a date component, it may be easier to skip the conversion into the Date Object. Time values, depending on the application of course, are much simpler to handle than dates so it may be worthwhile, or even necessary, to write your own time parsing and formatting functions. Examples of this are provided in the scripts.

Date/Time Manipulation:

The Date Object itself is useful for extracting specific information out of a date, such as the day of month, day of week, day of year, etc. and for doing some specific operations such as converting between time zones. But it cannot be used directly in a date calculation as in for example, finding the number of days between two dates. For this type calculation the millisecond value of the date is used. The Date Object provides functions for acquiring this millisecond value and for converting a millisecond value into a Date Object. So, after a date/time calculation is done,if applicable, the resulting millisecond value can be converted back into a Date Object for formatting or other operations.

Formatting Date/Time Strings:

There are many, many different ways to represent a date/time. It's important to have some general purpose way of generating these different formats. Acrobat JavaScript provides an excellent function for doing this, the "printd()" function. It takes as input a format string and a Data Object. The format string follows industry standard rules for representing a Date/Time. The following table shows these standard formatting strings and their meaning.
Date Formatting Time Formatting
mmmmMonth Name HH24 hour time, leading 0
mmmAbbreviated Month Name H24 hour time
mmNumeric Month, leading 0 hh12 hour time, leading 0
mNumeric Month h12 hour time
ddddDay of Week Name MMMinutes, leading 0
dddAbbreviated Day Name MMinutes
ddDay of Month, leading 0 ssSeconds, leading 0
dDay of Month sSeconds
yyyyYear, 4 digits ttam/pm indication
yyYear, 2 digits tam/pm, single character(a/p)

Here's an example of how the "util.printd()" function is used.

var curDate = new Date(); // Gets date object for right now
var strDate = util.printd("ddd, mmm dd, yyyy", curDate);

The variable "strDate" gets a value that looks like:
   Mon, Feb 22, 2009

The pages listed below provide specific scripts for handling different types of date and time operations.

Date and Time Calculations
Thom Parker
Several common date calculations, including finding age, the number of days and hours between two dates, calculating expiration dates, and many others. . . . keep reading
Parsing and Formatting Dates and Times
Thom Parker
Dates and times aren't like other kinds of values, because they can be represented in so many different ways. For example, the representation of a date or time used for a calculation is very different from the human readable form. These scripts show how to parse and format dates from any format into any other format so they can be used for both calculations and display. . . . keep reading
Entering Date/Time Strings: Part 2
Thom Parker
Scripts for setting up date/time entry fields. Includes explanations and scripts for automatically entering dates, and for using drop-down fields and a popup calendar. . . . keep reading
Entering Date/Time Strings: Part 1
Thom Parker
Part 1 of scripts for setting up date/time entry fields covers scripts to assist in manual date/time entry (keystroke, validation and formatting) and scripts to assist in automatic date/time entry (button and document scripts). . . . keep reading