JavaScript

Compare two timestamps by their numeric time

Compare Date.getTime values when checking the order of actual instants. The numeric result is milliseconds since the epoch, independent of how the date is displayed.

Worked example

Node.js example · save as example.mjs and run node example.mjs

const first = new Date("2026-06-01T10:00:00Z");
const second = new Date("2026-06-01T12:00:00+02:00");
console.log(first.getTime() === second.getTime());
console.log((second.getTime() - first.getTime()) / 1000);

How it works

Both strings describe the same instant using different offsets. Comparing their raw text would obscure that relationship. Numeric comparison also lets you compute elapsed duration without relying on locale-specific date formats.

Limits and mistakes

Invalid dates produce NaN, so validate before comparing. Elapsed milliseconds are different from calendar-day counts in a region with daylight-saving transitions. Decide whether your task concerns instants, dates, or local schedule times.

Check the result

Expect true and zero. Test timestamps with differing offsets and an invalid date. If you need whole local days, do not assume every day contains exactly 24 elapsed hours.

Sources

Reference documentation for the behavior used in this guide: