by Thom Parker, Software Developer/Adventurer, WindJack Solutions, Inc.
Part 1 of 3
Skill Level: Beginner to Intermediate
Material in the article applies to all Acrobat versions and variations.
Dates and times, both separately and together are useful, if not absolutely necessary, for a variety of PDF-document applications. The first, and probably the most common, use is for a date or time field on a form. Examples include the date on which the form was filled out, birthday, expiration date, start and/or end time, hours worked, days rented and so on. All of these fields require an expression of time displayed as either a date or time string.
Adobe Acrobat provides several built-in date-and-time formatting options. They are great for displaying a date/time, but what do you do if you want to use a date or time in a calculation, such as figuring out a due date or a rental cost? Or maybe you need to use the date to set various options on the form, such as changing the cost on a registration form as the event gets nearer. On a PDF form, these types of date-related operations can only be done programmatically.
So how do you work with dates and times programmatically in Acrobat?
var rightNow = new Date();
Your console window should look similar to Figure 1 below.
var rightNow = new Date();
This code uses what’s called the default constructor, which doesn’t have any input parameters. The default constructor for the Date object creates an object that represents the current date and time, so it’s what we will always use to get this information.
The Date object also includes functions for converting the raw date value into a human-readable string. We’ve already used one of these. Try a few of these other functions in the console window:
The next step is to use the date in a calculation. The Date object can’t be used directly in a mathematical expression. Instead, we convert it into its millisecond value, and then do the calculation with this value. If necessary, the result is converted back into a Date object. The following code adds five days to the current date and then prints the new date to the console window.
// Get the current date and time var rightNow = new Date(); // Get the millisecond value // of the date object var msRightNow = rightNow.getTime(); // Calculate 5 days in milliseconds, // 5 days x 24 hrs/day x 60 min/hr x 60 sec/min x 1000 ms/sec var fiveDays = 5 * 24 * 60 * 60 * 1000; // Do Calculation var finalTime = msRightNow + fiveDays; // Create a new Date from // the calculated value var theNewDate = new Date(finalTime); // In actual working code on a form you'll want to place the // result into a field value. But since this is test code we'll // Print the result to the Console Window (Great for Debugging) console.println("5 days from now is: " + theNewDate.toString());
In the example above, the ‘theNewDate’ variable is created using a different kind of date constructor than we used before. The Date object is created from the calculated millisecond value. This constructor gives us a way to create a Date object that represents any date and time. It’s useful here because we have a millisecond value as the result of a calculation, but what if we want to create a Date object from scratch using something a little more human readable than milliseconds? The Date object provides two constructors for this purpose.
var year = 2006, month = 3, day = 20; var hours = 10, minutes = 20, seconds = 0, ms = 0; var myDate = new Date(year,month,day,hours,minutes,seconds,ms); // use a date string var myDate = new Date("4/10/82");
It is often necessary to work with a span of time between two dates. The following example calculates a rental cost based on a start and end date. You can execute this code directly in the console window.
// Setup the start and end date objects // Nov 10th, 2005 var startDate = new Date(2005, 11, 10); // Jan 5th, 2006 var endDate = new Date(2006, 1, 5); // Calculate the time difference in milliseconds var diff = endDate.getTime() - startDate.getTime(); // Calculate the number of milliseconds in a day // 1 day x 24 hrs/day x 60 min/hr x 60 sec/min x 1000 ms/sec var oneDay = 24 * 60 * 60 * 1000; var price = 3.00; // $3.00/day; // Calculate the number of days from start to end // and the total cost var numOfDays = diff/oneDay; var totalCost = numOfDays * price; // Build a message for the user and print it to the console var strMessage = "# of days = " + numOfDays; strMessage += " : price/day= " + price; strMessage += " : total cost = " + totalCost; console.println(strMessage);
Go to Part 2
|Edit PDF create PDF Action Wizard|
Try Acrobat DC
Get started >
Post, discuss and be part of the Acrobat community.
Join now >