@ooneex/hour-utils turns a raw duration into a human-friendly breakdown. Pick a source unit (Hour, Minute, Second, or Millisecond), call .convert(value).to(format), and get back the requested components plus a ready-to-display text string. The chosen format is fully type-safe, so the returned object only contains the fields you asked for.
Installation
Add the package with Bun:Usage
Each unit class exposes a staticconvert method that returns a converter with a .to(format) call. The format string selects which components to compute: d (days), h (hours), m (minutes), s (seconds), combined as dhms, dhm, dh, d, hms, hm, h, ms, m, or s.
text field omits zero components, but always shows at least the smallest requested unit when everything is zero:
When to use it
- You have a duration in one unit and want to display it as days/hours/minutes/seconds (e.g. uptime, remaining time, elapsed time).
- You want a pre-formatted label (
text) without writing your own string-building logic. - You want the returned shape to be statically typed to the exact format you requested.
- Skip it if you only need a plain numeric conversion (e.g. hours to seconds is just
hours * 3600) or full calendar/timezone date math, for which a date library is more appropriate.