Other PDF Issues

Fix PDF JavaScript Date Calculation Error During Daylight Saving Time

Kushani NimanthikaKushani Nimanthika Nov 24, 2026 870 views

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.

How to Fix PDF JavaScript Date Calculation Errors During Daylight Saving Time
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Field Properties

Enter 'Prepare Form' or 'Form Edit' mode in your PDF editor, right-click the target calculation text field, and select 'Properties'.

2
Access the Calculation Script

Navigate to the 'Calculate' tab and choose the 'Custom calculation script' option. Click 'Edit' to open the JavaScript editor window.

3
Modify the Rounding Function

Locate the line calculating the total days and change 'Math.floor' to 'Math.round'. For example: 'var days = Math.round(diff / oneDay) + 1;'

4
Save and Test

Click 'OK' to save the script, close the properties window, and test the form by entering a date range that crosses a DST boundary.

Use Math.round() Instead of Math.floor()
Script Accuracy: Math.round() safely absorbs the 1-hour deviation without impacting normal 24-hour day calculations.
Advanced PDF Editing

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. 1. Open PDF in WPS Office: Launch WPS Office and open your fillable PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive field options.
  3. 3. Edit Field Properties: Double-click the text field where the calculation occurs to open its Properties dialog.
  4. 4. Update Custom Script: Under the 'Calculate' tab, select 'Custom calculation script', click 'Edit', and input your corrected JavaScript code.
Easily insert and manage interactive form fieldsHigh compatibility with standard PDF JS calculationsFree, lightweight, and fast performance across devices
microsoft office alternative - wps office

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.