If you haven’t done so already you should familiarize yourself with the Acrobat JavaScript Console so you can follow along and test the scripts for creating, displaying, and calculating date and time in JavaScript.
The Date Object
To create a date object run the following simple script in the console:
new Date();
The script above will create a date object at the current date and time. I ran the script as I’m putting the finishing touches on this article and it returned the following:
Fri Apr 26 2024 21:37:13 GMT-0400 (Eastern Standard Time)
The following script will return the number of milliseconds that have passed since midnight on Jan 1, 1970:
new Date().getTime();
I ran the script above in a loop ten times and printed the results in the console like this:
for (var i=0;i<10;i++)
{console.println(new Date().getTime())}
Try it. These are my results:
1714182465460
1714182465465
1714182465468
1714182465471
1714182465474
1714182465477
1714182465480
1714182465483
1714182465486
1714182465490
As you can see, the script executes every 3-5 milliseconds. Each line is the total number of milliseconds between Jan 1, 1970 at midnight and when the script was executed. This script can be used as part of a random number generating script.
Date Object that is not the Current Time
You can create a date object for any specific date by using the util.scan( ) function that takes two inputs:
The date format.
The specific date in the format that matches point 1.
For example, running the following script in the console will return the date and time for Jan 1, 2025. Since no minutes or seconds are in the format, it will return the time at midnight.
util.scand("mm/dd/yyyy", "01/01/2025");
You can also get the milliseconds since Jan 1, 1970 by running the following script:
util.scand("mm/dd/yyyy", "01/01/2025").getTime();
You can also create a date object for a specific date by using new Date( ) with the milliseconds as the input. For example, when I ran the script above it returned 1735707600330. When I used those milliseconds as an input into new Date( ) like this:
new Date(1735707600330);
it returned Wed Jan 01 2025 00:00:00 GMT-0500 (Eastern Daylight Time). When you use a date prior to Jan 1, 1970 to get the milliseconds it will return the milliseconds until that date, rather than the milliseconds since that date, expressed as a negative number.
util.scand("mm/dd/yyyy","02/14/1965").getTime()
//returns -153946799272
new Date(-153946799272)
// returns Sun Feb 14 1965 00:00:00 GMT-0500 (Eastern Daylight Time)
Displaying The Date Object in a Specific Format
To display a date in a specific format, use the util.printd( ) method which takes two inputs:
The format in which you want the date displayed.
The date object.
For example, if you want to display the current date (a date stamp) as mm/dd/yyyy, use the following script:
util.printd("mm/dd/yyyy", new Date());
You can use any date and time format combination with any separators you like. Here are different formats:
d 1-digit day (will display 2 digits for days 10 through 31)
dd 2-digit day (will display a leading zero for days 1 through 9)
ddd 3-letter day of the week (Mon, Tue, etc.)
dddd day of the week spelled out in full (Monday, Tuesday, etc)
m month in 1 digit (will still display 2 digits for Oct through Dec)
mm month in 2 digits (will display a leading zero for Jan through Sep)
mmm 3-letter month (Jan, Feb, etc.)
mmmm month spelled out in full (January, February, etc.)
yy 2-digit year (24)
yyyy 4-digit year (2024)
h 1-digit hour, 12-hour clock (will display 2 digits for 10-12)
hh 2-digit hour, 12-hour clock (will display a leading zero for 1-9)
H 1-digit hour, 24-hour clock (will display 2 digits for 10-23)
HH 2-digit hour, 24-hour clock (will display a leading zero for 1-9)
tt am or pm
M 1-digit minute (will display 2 digits for minutes 10-59)
MM 2-digit minute (will display a leading zero for minutes 10-59)
s 1-digit second (will display 2-digits for seconds 10-59)
ss 2-digit second (will display a leading zero for seconds 0-9)
Here are some examples:
util.printd("dd-mm-yyyy", new Date());
util.printd("mmmm d, yyyy", new Date());
util.printd("mm:dd:yyyy HH:MM:ss", new Date());
util.printd("mmmm dd, yyyy, h:MM tt", new Date());