Dev Helpers

JavaScript / Intervals and scheduling

Detect an overnight shift

Dev Helpers · July 18, 2026

A clock time after midnight may belong to yesterday's shift. Compare the interval endpoints before calculating its duration.

Worked example

const duration = (start, end) => (end - start + 1440) % 1440;
console.log(duration(22 * 60, 6 * 60));

Result

480

Implementation notes

Hours alone cannot capture daylight-saving transitions or shifts lasting 24 hours. Store dated instants when elapsed time affects pay or scheduling.

Try it yourself

Try an ordinary daytime shift, a shift crossing midnight and equal endpoints.

Open the JavaScript example →

Editorial date July 18, 2026 · Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.