Technical skill practice

JavaScript Time Difference: Compute Elapsed Time with Explicit Offsets

To calculate elapsed time, parse explicit instants and subtract their millisecond timestamps. A calendar-date change answers a different question and can occur after far less than twenty-four elapsed hours.

The task

Compare a booking start and end across midnight in a supplied +02:00 offset. Keep that offset explicit so the result does not depend on the computer’s local time zone.

Runnable Node 22 example

const assert = require('node:assert/strict');

const start = Date.parse('2026-10-20T23:30:00+02:00');
const end = Date.parse('2026-10-21T01:00:00+02:00');
assert(Number.isFinite(start) && Number.isFinite(end));
const milliseconds = end - start;
const hours = milliseconds / 3_600_000;
assert.equal(milliseconds, 5_400_000);
assert.equal(hours, 1.5);
assert.equal(Date.parse('2026-10-20T21:30:00Z'), start);
assert.equal(end - (start + 86_400_000), -81_000_000);
console.log(`milliseconds=${milliseconds}; hours=${hours}`);

Expected output and explanation

The interval spans ninety minutes, even though the displayed local calendar date changes. The UTC-equivalent assertion confirms that an explicit offset identifies the same starting instant, rather than creating a different duration.

A fixed twenty-four-hour interval is 86,400,000 milliseconds. That number should not be silently substituted for “next calendar day” in scheduling logic. Calendar operations require a chosen time zone and date policy, especially where offset changes can occur. This fixture avoids that ambiguity by supplying two explicit instants.

Output from the example

milliseconds=5400000; hours=1.5

Pitfall to check

Check parsing before subtracting, and decide what a negative duration means for your application. Do not apply Math.abs automatically if an end-before-start input should be rejected. Duration calculations cannot validate that a booking was approved.

Questions, answered

Should I use Date.now for precise runtime benchmarking?

Wall-clock timestamps can change. For performance measurements, use a suitable monotonic timing API rather than treating calendar time as a precision benchmark.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.