Calculate the Difference in Days Between Two Dates

To calculate the number of calendar days between two dates, normalize each year/month/day to UTC midnight, subtract the timestamps, and divide by 86,400,000. Using UTC avoids daylight-saving transitions turning a local calendar day into 23 or 25 elapsed hours.

const MS_PER_DAY = 86_400_000;
const start = Date.UTC(2026, 8, 10);
const end = Date.UTC(2026, 8, 18);
console.log((end - start) / MS_PER_DAY); // 8
Choose two dates.

Read the Two Date Values Top ↑

The original project read two text fields and used split(). An input type="date" gives a predictable YYYY-MM-DD value, which can still be split into numeric parts. Those parts can then be used with the JavaScript Date object or normalized directly with Date.UTC().

const [year, month, day] = value.split("-").map(Number);

Normalize Each Date to a UTC Calendar Day Top ↑

The older approach created local Date objects and divided the timestamp difference by 24 hours. That can be off around daylight-saving changes. For date-only arithmetic, Date.UTC() gives a stable midnight reference for each calendar date.

function toUtcDay(value) {
  const [year, month, day] = value.split("-").map(Number);
  return Date.UTC(year, month - 1, day);
}

Signed vs Absolute Difference Top ↑

A signed difference tells you direction: positive means the end date is later; negative means it is earlier. Use Math.abs() only when direction does not matter.

const signedDays = (endUtc - startUtc) / MS_PER_DAY;
const absoluteDays = Math.abs(signedDays);

Calendar Days vs Elapsed 24-Hour Periods Top ↑

“Days between calendar dates” and “complete 24-hour periods between two date-times” are different questions. UTC-midnight normalization is appropriate for calendar dates. For exact elapsed time between timestamps, subtract the actual timestamps and keep the result in milliseconds or the required duration units.

const elapsedMs = endDate.getTime() - startDate.getTime();
const elapsedHours = elapsedMs / (60 * 60 * 1000);

Validation and Common Mistakes Top ↑

  • Do not silently accept an empty or malformed value.
  • Do not subtract local midnights and assume every local day contains exactly 24 hours.
  • Do not apply Math.floor() or Math.round() to hide a timezone error; normalize the calendar dates correctly first.
  • Decide whether the result should be signed or absolute before displaying it.

Using Calendar Inputs Top ↑

The same calculation can be connected to the custom date-picker projects. See the browsable date-picker tutorial and the existing calendar date-difference demo.

The original downloadable example is still available: Download JavaScript date difference source code

Date Reference All Date Projects




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer