1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/utils/formatDuration.ts
Sebastian Malton b1ff34879a cleanup Lens repo with tighter linting
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
2020-07-09 17:00:23 -04:00

26 lines
625 B
TypeScript

// Formatting date duration in shorten format, e.g. "2d", or "25m"
import moment from "moment";
export function formatDuration(timeValue: number, compact: boolean): string {
let result = "";
const duration = moment.duration(timeValue);
const suffixes = ["d", "h", "m"];
const durationValues = [
Math.round(duration.asDays()),
duration.hours(),
duration.minutes(),
];
durationValues.forEach((value, index) => {
if (value) {
result += value + suffixes[index] + " ";
}
});
if (compact) {
result = result.split(" ")[0];
}
if (!result) {
return "<1m";
}
return result;
}