Created
August 27, 2024 14:40
-
-
Save eoguvo/5e8aa0f597cda48f158b30982092e471 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isNumeric } from "@core/Utils/index"; | |
const DateDuration = function (milliseconds?: number, hasSeconds = true, hasMinutes = true): string { | |
if (!milliseconds) { | |
return "0s"; | |
} | |
if (milliseconds <= 0 || milliseconds === Infinity) { | |
return "0s"; | |
} | |
const totalSeconds = Math.floor(milliseconds / 1000); | |
const hours = Math.floor(totalSeconds / 3600); | |
const minutes = Math.floor((totalSeconds % 3600) / 60); | |
const seconds = totalSeconds % 60; | |
const timeHour = (hours > 0) ? `${ hours }h ` : ""; | |
const timeMinute = (minutes > 0) ? `${ minutes }min ` : ""; | |
if (!hasMinutes) { | |
return timeHour || "0h"; | |
} | |
if (!hasSeconds) { | |
const timeConverted = `${ timeHour }${ timeMinute }`; | |
return timeConverted || "0s"; | |
} | |
const timeSecond = (isNumeric(seconds)) ? `${ seconds }s` : "0s"; | |
const timeConverted = `${ timeHour }${ timeMinute }${ timeSecond }`; | |
return timeConverted; | |
}; | |
export default DateDuration; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment