Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Date and Intl

Date arithmetic

How to add days and months, find the difference between dates and avoid daylight saving and month-end bugs when doing date arithmetic in JavaScript.

JavaScript has no addDays() or diff() methods. Date arithmetic means combining a few primitives: converting a date to a number of milliseconds, using the getters and setters, and letting the engine roll over values that are out of range. It is simple once you know three facts.

First, a Date is a timestamp, so subtracting two dates gives milliseconds. Second, setters accept values such as day 35 and fix them up. Third, a "day" is not always 24 hours in local time, because daylight saving time makes some days 23 or 25 hours long. Most date bugs come from forgetting the third fact.

Differences: subtract timestamps

Subtracting two dates converts both to numbers. Divide by the size of a unit to get seconds, minutes, hours or days.

JS Differences between dates

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The daysBetween() helper first converts each date to a UTC midnight. In UTC every day is exactly 24 hours, so the division is exact. The calendar day is read with the local getters, so the answer matches what the user sees on a calendar.

Adding days, months and years

The reliable way to add calendar units is to read a part, add to it and write it back. The setter rolls extra days into the following months.

JS Add days and months safely

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The examples use the UTC versions of the methods so they give the same answer on every computer. For dates a person sees on a calendar (a birthday, a deadline), the local versions are the right ones.

The pitfalls

  • A day is not always 86,400,000 ms. In a time zone with daylight saving time, adding that many milliseconds to "9:00 on Saturday" may land on 8:00 or 10:00 on Sunday. If you mean "the same time tomorrow", use setDate(getDate() + 1) on a local date.
  • Dates are mutable. setDate() and friends change the object. Copy with new Date(date) first, or your helper will change its caller's data.
  • Month-end overflow. Adding one month to January 31 gives March 2 or 3, not the end of February. Clamp the day as addMonths() does.
  • Months count from 0, days from 1. Day 0 is the last day of the previous month, a handy trick for finding the number of days in a month: new Date(year, month + 1, 0).getDate().
  • Date-only strings are UTC while date-time strings are local, so new Date("2026-03-15") and new Date("2026-03-15T00:00") differ by the offset.
  • Comparing: use <, > or getTime(). Never ===.
  • Exact elapsed time (a stopwatch, a timeout) should use performance.now(), because the system clock can be adjusted.
  • Leap years and leap seconds. Date handles leap years (Feb 29) correctly; it ignores leap seconds, so every day has exactly 86,400 seconds in UTC.

Beyond Date

When your app needs durations, time zones other than the local one, or calendar math such as "three months after this date", the Temporal API provides add(), subtract() and until() with these rules built in. Where it is not available yet, a small set of helper functions like the ones above is usually enough.

Browser support

Everything used on this page (Date, its getters and setters, and Date.UTC()) works in every browser and runtime. Temporal is recent, so check a compatibility table.