Fix PDF JavaScript Date Calculation Error During Daylight Saving Time
Question details
A JavaScript-based date calculation in a PDF form returns a result that is one day short when the selected date range crosses over a Daylight Saving Time (DST) change.

- Product
- PDF Form Editor
- Device & OS
- not provided
- Scenario
- Calculating the total number of days between two dates using a custom JavaScript calculation inside a fillable PDF form.
- Observed behavior
- The calculated day difference is exactly one day shorter than expected because the DST transition reduces the duration of one day to 23 hours, causing Math.floor() to incorrectly round down.
Ensure you have access to a PDF editor with form-editing capabilities so you can modify the custom calculation script inside the specific form field.
Use Math.round() Instead of Math.floor()
Replacing Math.floor() with Math.round() prevents the calculation from rounding down due to the missing hour during the DST shift.
Because a DST transition day contains 23 hours rather than 24, dividing the millisecond difference by standard 24-hour milliseconds results in a fractional value (e.g., 5.95 days instead of 6.0 days). Using Math.floor() drops the decimal, erroneously resulting in 5 days.
Changing the script to round to the nearest whole number ensures the missing hour does not affect the final day count.
Enter 'Prepare Form' or 'Form Edit' mode in your PDF editor, right-click the target calculation text field, and select 'Properties'.
Navigate to the 'Calculate' tab and choose the 'Custom calculation script' option. Click 'Edit' to open the JavaScript editor window.
Locate the line calculating the total days and change 'Math.floor' to 'Math.round'. For example: 'var days = Math.round(diff / oneDay) + 1;'
Click 'OK' to save the script, close the properties window, and test the form by entering a date range that crosses a DST boundary.

Normalize Both Dates to Noon Before Calculation
Setting the time component of both start and end dates to 12:00 PM (noon) safely bypasses the 2:00 AM DST shift entirely.
Edit PDF Forms and Scripts with WPS Office
WPS Office offers robust PDF editing capabilities, including the ability to create fillable forms, define properties, and execute custom JavaScript calculations smoothly without format compatibility issues.
- 1. Open PDF in WPS Office: Launch WPS Office and open your fillable PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive field options.
- 3. Edit Field Properties: Double-click the text field where the calculation occurs to open its Properties dialog.
- 4. Update Custom Script: Under the 'Calculate' tab, select 'Custom calculation script', click 'Edit', and input your corrected JavaScript code.

Frequently Asked Questions
Why does Daylight Saving Time affect JavaScript dates in PDFs?
During a DST change, the clocks 'spring forward', effectively making that specific calendar day 23 hours long instead of 24. When your script divides the total milliseconds by a fixed 24-hour millisecond value, the fractional day gets rounded down incorrectly if you use Math.floor().
Where do I enter JavaScript for a PDF form field?
In your PDF editor's form preparation mode, right-click the target field and select Properties. Navigate to the Calculate tab, select the 'Custom calculation script' option, and click Edit to type or paste your JavaScript code.
Can I test my PDF JavaScript without publishing the form?
Yes. Most PDF editors, including WPS Office, allow you to switch from Form Editing mode to Hand/Preview mode. This allows you to interact with the fields normally and instantly verify if your date calculation script works correctly.




