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
480Implementation 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