Closed hovancik closed 9 months ago
HumanizeDuration.js doesn't support this feature, unfortunately. This library is also in maintenance mode so I don't intend to add new features. I'm going to close this issue as a result.
Here's a solution that might work for you, which uses the parse-ms
module:
import parseMilliseconds from "parse-ms";
const UNITS = ["hours", "minutes", "seconds"];
function humanize(ms) {
const parsed = parseMilliseconds(ms);
const result = [];
let hasSeenNonzeroUnit = false;
for (const unit of UNITS) {
const unitCount = parsed[unit];
if (unitCount || hasSeenNonzeroUnit) {
result.push(`${unitCount} ${unit}`);
}
if (unitCount) hasSeenNonzeroUnit = true;
}
return result.join(" ");
}
console.log(humanize(10845678));
// => 3 hours 0 minutes 45 seconds
console.log(humanize(12345));
// => 12 seconds
Also happy to mention a fork in my README if you want to maintain one.
Let's say we have
Would be nice if following could be done: