Skip to content

Commit 774f8f1

Browse files
committed
feat: update getTimeDiff function
1 parent 2989e77 commit 774f8f1

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/Shared/Helpers.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -790,19 +790,24 @@ export const getTimeDifference = ({
790790
return fallbackString
791791
}
792792

793-
const seconds = moment(endTime).diff(moment(startTime), 'seconds')
794-
const minutes = moment(endTime).diff(moment(startTime), 'minutes')
795-
const hours = moment(endTime).diff(moment(startTime), 'hours')
793+
const duration = moment.duration(moment(endTime).diff(moment(startTime)))
796794

797-
if (seconds < 60) {
798-
return `${seconds}s`
799-
}
800-
if (minutes < 60) {
801-
return `${minutes}m ${seconds % 60}s`
795+
const units = [
796+
{ label: 'd', value: duration.days() },
797+
{ label: 'h', value: duration.hours() },
798+
{ label: 'm', value: duration.minutes() },
799+
{ label: 's', value: duration.seconds() },
800+
]
801+
802+
// Filter out zero values and take the first two non-zero units
803+
const nonZeroUnits = units.filter((unit) => unit.value > 0).slice(0, 2)
804+
805+
// If all units are zero, show "0s"
806+
if (nonZeroUnits.length === 0) {
807+
return '0s'
802808
}
803-
const leftOverMinutes = minutes - hours * 60
804-
const leftOverSeconds = seconds - minutes * 60
805-
return `${hours}h ${leftOverMinutes}m ${leftOverSeconds}s`
809+
810+
return nonZeroUnits.map((unit) => `${unit.value}${unit.label}`).join(' ')
806811
}
807812

808813
export const getFileNameFromHeaders = (headers: Headers) =>

0 commit comments

Comments
 (0)