JavaScript Date Difference Calculator
Generate JavaScript for calendar-day or elapsed-day counts.
How the JavaScript Date Difference Calculator Works
The JavaScript Date Difference Calculator writes a JavaScript snippet that subtracts 2 dates in milliseconds and converts the gap to days.
Enter the start date
Enter the earlier date.
Enter the end date
Enter the later date.
Generate the snippet
The calculator writes code with Date.parse() and Math.round().
Paste and run
Run the code in a browser console or Node.js.
How the Count Is Calculated
JavaScript stores dates as milliseconds since the Unix epoch.
const ms = Date.UTC(y2, m2 - 1, d2) - Date.UTC(y1, m1 - 1, d1);
const days = Math.round(ms / 86400000);- Date.UTC()
- Builds a UTC timestamp. Months start at 0 for January.
- 86400000
- Milliseconds per day (24 × 60 × 60 × 1,000).
- Math.round()
- Removes the 1-hour error that local-time dates gain across DST changes.
Example: Date.UTC(2026, 11, 25) − Date.UTC(2026, 11, 4) = 1,814,400,000 ms. 1,814,400,000 ÷ 86,400,000 = 21 days.
Worked Examples
The table lists JavaScript date pitfalls and safe patterns.
| Code | Result | Note |
|---|---|---|
| new Date("2026-12-04") | UTC midnight | Date-only ISO strings parse as UTC. |
| new Date("2026-12-04T00:00") | Local midnight | Date-time strings without Z parse as local time. |
| new Date(2026, 11, 4) | 4 Dec 2026 | Month 11 is December. |
| new Date(2026, 0, 31 + 30) | 2 Mar 2026 | Day overflow rolls into the next months. |
| (b - a) / 86400000 across DST | 20.958… | Use Math.round() or Date.UTC(). |
What the Tool Measures
A JavaScript Date Difference Calculator is a code generator that produces JavaScript for counting days between 2 dates.
Local-time dates in JavaScript include DST shifts, so a span across a spring clock change is 1 hour short of whole days. UTC dates and rounding return whole calendar days. The Temporal API adds Temporal.PlainDate.until() for calendar-safe differences.
When to Use This Tool
There are 4 main uses of the JavaScript Date Difference Calculator, including countdown widgets and form validation.
Countdown widgets
Front-end developers show days until a launch.
Form validation
Forms check that a return date follows a departure date.
Booking engines
Sites compute nights between check-in and check-out.
Node.js jobs
Back-end jobs expire records older than N days.