diff --git a/src/datetime/seconds_to_duration.ts b/src/datetime/seconds_to_duration.ts index bb74f1f..1d076a8 100644 --- a/src/datetime/seconds_to_duration.ts +++ b/src/datetime/seconds_to_duration.ts @@ -1,18 +1,26 @@ const leftPad = (num: number) => (num < 10 ? `0${num}` : num); -export function secondsToDuration(d: number) { - const h = Math.floor(d / 3600); - const m = Math.floor((d % 3600) / 60); - const s = Math.floor((d % 3600) % 60); +export function secondsToDuration(duration: number) { + if (duration < 1) return null; + let adjDuration = duration; + const d = Math.floor(adjDuration / 86400); + adjDuration -= d*86400; + const h = Math.floor(adjDuration / 3600); + adjDuration -= h*3600; + const m = Math.floor(adjDuration / 60); + adjDuration -= m*60; + const s = Math.floor(adjDuration); - if (h > 0) { - return `${h}:${leftPad(m)}:${leftPad(s)}`; + // Constructing string from seconds upwards + let string = "" + leftPad(s); + if (duration >= 60) { + string = `${leftPad(m)}:` + string + if (duration >= 3600) { + string = `${leftPad(h)}:` + string + if (duration >= 86400) { + string = `${leftPad(d)}:` + string + } + } } - if (m > 0) { - return `${m}:${leftPad(s)}`; - } - if (s > 0) { - return "" + s; - } - return null; + return string; }