diff --git a/src/timehelper.ts b/src/timehelper.ts index 34af7c3..b5d3d69 100644 --- a/src/timehelper.ts +++ b/src/timehelper.ts @@ -51,30 +51,36 @@ export function calculateSecondsBetween( return { seconds, toReadable: (roundToMinutes?: boolean) => - secondsToReadable(seconds, roundToMinutes), + formatSecondsToDHMS(seconds, roundToMinutes), }; } -export function secondsToReadable( +export function formatSecondsToDHMS( secs: number, roundToMinutes: boolean = false, ): string { const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs; - var days = Math.floor(totalSeconds / (3600 * 24)); - var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600); - var minutes = Math.floor((totalSeconds % 3600) / 60); - var seconds = Math.floor(totalSeconds % 60); + const days = Math.floor(totalSeconds / (3600 * 24)); + const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600); + const minutes = Math.floor((totalSeconds % 3600) / 60); + const seconds = Math.floor(totalSeconds % 60); - var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : ""; - var hourDisplay = + const dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : ""; + const hourDisplay = hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : ""; - var minuteDisplay = + const minuteDisplay = minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : ""; - var secondDisplay = + const secondDisplay = seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : ""; return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace( /,\s*$/, "", ); } + +export function formatSecondsToMS(s: number): string { + const mins = Math.floor(s / 60); + const secs = s % 60; + return `${mins}:${secs < 10 ? "0" : ""}${secs}`; +}