Skip to main content

JavaScript Dates

JavaScript Date Arithmetic

Written by Published

Dates can be compared and subtracted because they are numbers underneath.

Subtracting two dates gives the difference in milliseconds.

Dividing that number converts it to days, hours or minutes.

Example

Example

javascript

const start = new Date(Date.UTC(2026, 0, 1));
const end = new Date(Date.UTC(2026, 0, 15));

const days = (end - start) / (1000 * 60 * 60 * 24);

console.log(days);

The output is 14.

Working with Differences

Subtracting dates gives milliseconds.

One day is 1000 times 60 times 60 times 24 milliseconds.

Comparison operators work directly on dates.

Syntax

Syntax

javascript

const ms = laterDate - earlierDate;
const days = ms / 86400000;

=== does not work; compare timestamps instead.

Adding Days

setUTCDate rolls over into the next month by itself.

Example

Example

javascript

const date = new Date(Date.UTC(2026, 0, 30));

date.setUTCDate(date.getUTCDate() + 5);

console.log(date.getUTCMonth());
console.log(date.getUTCDate());

January 30 plus 5 days becomes February 4.

Comparing Dates

Less than and greater than work as expected.

=== does not, because two Date objects are never identical.

Example

Example

javascript

const a = new Date(Date.UTC(2026, 0, 1));
const b = new Date(Date.UTC(2026, 0, 15));
const c = new Date(Date.UTC(2026, 0, 1));

console.log(a < b);
console.log(a === c);
console.log(a.getTime() === c.getTime());

Compare getTime() to test for the same moment.

Days Between Two Dates

Divide the difference and round to handle daylight saving.

Example

Example

javascript

function daysBetween(a, b) {
  return Math.round((b - a) / 86400000);
}

const start = new Date(Date.UTC(2026, 0, 1));
const end = new Date(Date.UTC(2026, 1, 1));

console.log(daysBetween(start, end));

January has 31 days, so that is the answer.

Copy Before Changing

The setters change the date in place, which is easy to forget.

Example

Example

javascript

const original = new Date(Date.UTC(2026, 0, 1));
const later = new Date(original.getTime());

later.setUTCDate(later.getUTCDate() + 10);

console.log(original.getUTCDate());
console.log(later.getUTCDate());

The output is 1 then 11.

Month Arithmetic Is Awkward

Adding a month to the 31st can land in the following month.

This is where a date library starts to earn its place.

Example

Example

javascript

const date = new Date(Date.UTC(2026, 0, 31));

date.setUTCMonth(date.getUTCMonth() + 1);

console.log(date.getUTCMonth());
console.log(date.getUTCDate());

February has no 31st, so it rolled into March.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Date Arithmetic</title>
</head>
<body>

  <h1>Date Arithmetic</h1>

  <p id="out"></p>

  <script>
    const start = new Date(Date.UTC(2026, 0, 1));
    const end = new Date(Date.UTC(2026, 1, 1));

    const days = Math.round((end - start) / 86400000);

    const later = new Date(start.getTime());
    later.setUTCDate(later.getUTCDate() + 10);

    document.getElementById("out").innerHTML =
      "Days between: " + days +
      "<br>Ten days later: " + later.toISOString().slice(0, 10) +
      "<br>Start is earlier: " + (start < end);
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try a different gap:

Change the end month and watch the day count change.

Important Points

  • Subtracting dates gives milliseconds.
  • One day is 86400000 milliseconds.
  • < and > work on dates directly.
  • === does not; compare getTime().
  • Setters change the date in place, so copy first.

Conclusion

Date arithmetic works because a date is a number underneath.

Comparing with === is the mistake people make first.

Month arithmetic is genuinely awkward, and a library helps.