How to Compare Dates and Return the Last Day of the Following Month in a PDF Form
Question details
The user needs a custom JavaScript calculation in a fillable PDF form to compare a 'From Date' and a 'To Date', and return the last day of the month following the 'From Date' if the condition is met.

- Product
- Device & OS
- not provided
- Scenario
- Automating field calculations in a PDF form based on custom date logic and conditional comparisons.
- Observed behavior
- The existing script fails to return the expected calculated date due to syntax errors and improper use of string formatting functions instead of dedicated date formatting methods.
Ensure that your 'From Date' and 'To Date' form fields are configured to enforce a strict date format (e.g., 'dd-mmm-yy') so the JavaScript parser can read the values consistently without errors.
Use util.scand and util.printd for Custom Date Calculation
Parse the user-entered date strings into JavaScript Date objects, apply conditional logic, calculate the required end-of-month date, and format the output correctly.
In PDF JavaScript, standard string formatting tools like util.printf are not suitable for handling Date objects. You must use util.scand to read the date from the form field and util.printd to output the calculated Date object back into a formatted string.
To find the last day of the following month, you can dynamically set the month to the current month plus two, and set the day to zero.
Open your PDF form in editing mode. Right-click the target field where you want the result to appear and select 'Properties'.
Navigate to the 'Calculate' tab in the Properties window. Select the 'Custom calculation script' radio button and click 'Edit'.
Write JavaScript to retrieve and parse the input fields using util.scand. For example: var d1 = util.scand("dd-mmm-yy", this.getField("From Date").value); var d2 = util.scand("dd-mmm-yy", this.getField("To Date").value);
Add a conditional statement to compare d1 and d2. If d1 <= d2, calculate the last day of the following month using: d1.setMonth(d1.getMonth() + 2, 0);.
Assign the correctly formatted result to the current field using util.printd. Example: event.value = util.printd("dd-mmm-yy", d1);. If the condition fails, fallback to printing d2.

Create Smart PDF Forms with WPS Office
WPS PDF provides robust form creation tools, allowing you to easily add text fields, configure custom JavaScript calculations, and set complex date formats efficiently.
- 1. Open the PDF File: Launch WPS Office and open your fillable PDF document.
- 2. Access Form Tools: Click on the 'Form' tab in the top ribbon to reveal all interactive form tools.
- 3. Edit Field Properties: Right-click the specific text field that requires the date logic, and choose 'Properties'.
- 4. Add Calculation Script: Go to the 'Calculate' tab, select 'Custom calculation script', enter your standard PDF JavaScript code, and click 'Close' to save.

Frequently Asked Questions
Why is my PDF date calculation returning an empty value?
This usually occurs when the format string provided to util.scand() does not exactly match the format of the date string retrieved from the field. If they mismatch, the system cannot parse the date and returns null.
How do I calculate the last day of a specific month in JavaScript?
In JavaScript, setting the day parameter of the setMonth() or setDate() method to 0 automatically shifts the date to the last day of the previous month. To get the last day of the following month, you add 2 to the current month and set the day to 0.
What is the difference between util.printf and util.printd in PDF forms?
util.printd is designed specifically for converting JavaScript Date objects into formatted strings in PDF forms. util.printf is used to format regular numbers, decimals, and text strings, and will fail if passed a Date object.
Are Adobe Acrobat JavaScripts compatible with other PDF editors like WPS?
Yes, standard PDF JavaScript objects and methods such as util.scand, util.printd, and event.value are universal to the PDF specification and work seamlessly in modern editors like WPS PDF.




