PDF Editing Issues

How to Compare PDF Dates and Close Documents with Acrobat JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Nov 10, 2026 868 views

Question details

The user wants to use Acrobat JavaScript to compare the current date with a specified deadline field in a PDF, and automatically close the document if the deadline has expired.

How to Compare PDF Dates and Close a Document Using Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Setting up an expiration or deadline mechanism within an interactive PDF form.
Observed behavior
A script is required to parse dates, compare their timestamps, display an alert, and execute form reset and document closure without triggering save prompts.
Before you start

Ensure your PDF editor supports advanced document-level JavaScript and has JavaScript execution enabled in its security settings. Note the exact field names of your date and deadline fields before writing the script.

Solution 1Recommended

Use a Custom Calculation Script to Compare Dates and Close the PDF

This method uses the util.scand function to parse date fields and executes the closeDoc method when the specified deadline has expired.

Acrobat JavaScript provides built-in methods to handle date conversions and document actions. By combining util.scand for date parsing and this.closeDoc for document management, you can create automated expiration features.

1
Create the date fields

In your PDF editor's form preparation mode, add two text fields. Name one 'CurrentDate' and the other 'DeadlineDate', ensuring both are formatted to accept dates.

2
Open the JavaScript editor

Right-click the field where the calculation will be triggered, select 'Properties' from the context menu, and navigate to the 'Calculate' tab.

3
Select custom calculation

Choose the 'Custom calculation script' radio button and click the 'Edit' button to open the JavaScript editor window.

4
Enter the parsing logic

Use the util.scand method to parse your date string values into standard JavaScript Date objects. For example: var deadline = util.scand('mm/dd/yyyy', this.getField('DeadlineDate').value);

5
Add the comparison and close commands

Write an if-statement comparing the date timestamps. If the current date is greater than the deadline, trigger app.alert('Deadline passed'); followed by this.resetForm();, this.dirty = false;, and this.closeDoc(true);

Use a Custom Calculation Script to Compare Dates and Close the PDF
Testing Warning: Always test this script on a duplicate copy of your document. Setting this.dirty = false prevents the save prompt, meaning any unsaved changes will be lost immediately when the document closes automatically.
Manage PDFs with WPS Office

Easily Edit and Manage PDF Forms with WPS Office

WPS Office includes a comprehensive PDF editor that allows you to effortlessly create, fill out, and manage PDF forms. It provides a highly compatible and user-friendly interface for all your document tasks without needing complex scripting.

  1. 1. Open WPS Office: Launch the WPS Office application and click on 'PDF Edit' from the main home screen.
  2. 2. Open your PDF: Select the target PDF file you want to edit and open it within the application.
  3. 3. Use Form Tools: Navigate to the 'Form' tab on the top ribbon to easily add interactive text fields, check boxes, or date fields.
  4. 4. Save and Export: Once your form fields are configured, click 'Save' to ensure your document retains its interactive elements perfectly.
Lightweight and efficient PDF reader and editorSeamless compatibility with standard PDF formatsEasily insert text fields, date pickers, and interactive formsAll-in-one suite for Word, Excel, PowerPoint, and PDF
microsoft office alternative - wps office

Frequently Asked Questions

What is the util.scand function in Acrobat JavaScript?

util.scand is a built-in Acrobat JavaScript method used to parse a specific date string into a standard JavaScript Date object based on a provided format string (like 'mm/dd/yyyy'). This makes it easier to accurately compare two different dates.

Why use this.dirty = false before closing a PDF?

Setting this.dirty = false explicitly tells the PDF viewer that the document has no unsaved modifications. This prevents the application from interrupting the script with a 'Do you want to save changes?' prompt when this.closeDoc() is executed.

Can I run this JavaScript deadline script in any PDF reader?

No, advanced document-level JavaScript commands like this.closeDoc() are primarily supported by robust PDF editors like Adobe Acrobat. Many third-party PDF viewers, mobile apps, or web browser extensions may disable or block these scripts for security reasons.