Skip to content

Instantly share code, notes, and snippets.

@eoguvo
Created August 27, 2024 14:40
Show Gist options
  • Save eoguvo/5e8aa0f597cda48f158b30982092e471 to your computer and use it in GitHub Desktop.
Save eoguvo/5e8aa0f597cda48f158b30982092e471 to your computer and use it in GitHub Desktop.
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