From ed5f1e0ad2443a7983147a4bc4d338aa143a695f Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:04:59 -0500 Subject: [PATCH] Add function to convert seconds to human readable duration --- web/src/utils/dateUtil.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/web/src/utils/dateUtil.ts b/web/src/utils/dateUtil.ts index dc5589884..8509155e1 100644 --- a/web/src/utils/dateUtil.ts +++ b/web/src/utils/dateUtil.ts @@ -229,6 +229,23 @@ export const getDurationFromTimestamps = ( return duration; }; +/** + * + * @param seconds - number of seconds to convert into hours, minutes and seconds + * @returns string - formatted duration in hours, minutes and seconds + */ +export const formatSecondsToDuration = (seconds: number): string => { + if (isNaN(seconds) || seconds < 0) { + return "Invalid duration"; + } + + const duration = intervalToDuration({ start: 0, end: seconds * 1000 }); + return formatDuration(duration, { + format: ["hours", "minutes", "seconds"], + delimiter: ", ", + }); +}; + /** * Adapted from https://stackoverflow.com/a/29268535 this takes a timezone string and * returns the offset of that timezone from UTC in minutes.