Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`
by sindresorhusJavaScript
Last 12 weeks · 1 commit
4 of 6 standards met
With , the sub-second component is rounded independently of the seconds, so a fractional value near the boundary rounds up to and is emitted as its own unit — an impossible combination: The milliseconds unit only ranges 0–999, so should never appear. This is the same "rounding rolls a unit past its max" class as the seconds→ issue (#38), which is handled for seconds by flooring (), but the separate-milliseconds component still used a bare . Fix Cap the rounded component at so it stays within range: Output becomes / — valid unit combinations, consistent with how the seconds component is kept below 60. Non-boundary values are unaffected ( → , → ). Tests Three regression cases added to the test; they produce the impossible output before the change and pass after. Full suite green.
I have the following situation: 1. I have and 2. I display , , and to the end-user 3. The result looks wrong because (conceptually) does not equal (due to rounding) Example: Wondering how you'd suggest to handle this? One idea: expose the rounded numeric value alongside the string (e.g. a -style API, or output), so callers can compute the delta from the values as displayed instead of from the raw inputs. Alternatively, an option to format a set of related values consistently (shared unit and precision). The same issue happens in .
@sindresorhus Would you accept a PR that takes an option to keep the decimals on whole numbers? I'm using this library for a progress indicator thingy which keeps updating the time spent every ~60ms or so, and it keeps flickering in the terminal when it hits a whole number - I'd prefer if it just kept the fixed length as specified. @sindresorhus For the same reason, I would find it preferable if sub-seconds could be output as seconds. I.e. -> . Right now I'm wrapping and adding the logic myself, but does it qualify for an option to be included in the library? _Originally posted by @ulken in https://github.com/sindresorhus/pretty-ms/issues/20#issuecomment-1825457807_
Repository: sindresorhus/pretty-ms. Description: Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s` Stars: 1215, Forks: 69. Primary language: JavaScript. Languages: JavaScript (96.2%), TypeScript (3.8%). License: MIT. Topics: humanize, milliseconds, npm-package. Latest release: v9.3.0 (11mo ago). Open PRs: 1, open issues: 0. Last activity: 1mo ago. Community health: 85%. Top contributors: sindresorhus, fisker, icyflame, villebro, arnellebalane, Richienb, kevva, ehmicky, OyuNet, Rich-Harris and others.