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.