15 lines
502 B
TypeScript
15 lines
502 B
TypeScript
/** Human duration between two ISO datetimes, e.g. "23 min", "2 h", "1 h 30". */
|
|
export const eventDuration = (
|
|
startIso?: string | null,
|
|
endIso?: string | null,
|
|
): string | null => {
|
|
if (!startIso || !endIso) return null
|
|
const d = Math.round(
|
|
(new Date(endIso).getTime() - new Date(startIso).getTime()) / 60000,
|
|
)
|
|
if (d <= 0) return null
|
|
if (d < 60) return `${d} min`
|
|
const h = Math.floor(d / 60)
|
|
const m = d % 60
|
|
return m ? `${h} h ${String(m).padStart(2, "0")}` : `${h} h`
|
|
}
|