市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.9 KiB

  1. # pretty-ms
  2. > Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`
  3. ## Install
  4. ```sh
  5. npm install pretty-ms
  6. ```
  7. ## Usage
  8. ```js
  9. import prettyMilliseconds from 'pretty-ms';
  10. prettyMilliseconds(1337000000);
  11. //=> '15d 11h 23m 20s'
  12. prettyMilliseconds(1337000000n);
  13. //=> '15d 11h 23m 20s'
  14. prettyMilliseconds(1337);
  15. //=> '1.3s'
  16. prettyMilliseconds(133);
  17. //=> '133ms'
  18. // `compact` option
  19. prettyMilliseconds(1337, {compact: true});
  20. //=> '1s'
  21. // `verbose` option
  22. prettyMilliseconds(1335669000, {verbose: true});
  23. //=> '15 days 11 hours 1 minute 9 seconds'
  24. // `colonNotation` option
  25. prettyMilliseconds(95500, {colonNotation: true});
  26. //=> '1:35.5'
  27. // `formatSubMilliseconds` option
  28. prettyMilliseconds(100.400080, {formatSubMilliseconds: true})
  29. //=> '100ms 400µs 80ns'
  30. // `subSecondsAsDecimals` option
  31. prettyMilliseconds(900, {subSecondsAsDecimals: true});
  32. //=> '0.9s'
  33. // Can be useful for time durations
  34. prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
  35. //=> '35m'
  36. ```
  37. ## API
  38. ### prettyMilliseconds(milliseconds, options?)
  39. #### milliseconds
  40. Type: `number | bigint`
  41. Milliseconds to humanize.
  42. #### options
  43. Type: `object`
  44. ##### secondsDecimalDigits
  45. Type: `number`\
  46. Default: `1`
  47. Number of digits to appear after the seconds decimal point.
  48. ##### millisecondsDecimalDigits
  49. Type: `number`\
  50. Default: `0`
  51. Number of digits to appear after the milliseconds decimal point.
  52. Useful in combination with [`process.hrtime()`](https://nodejs.org/api/process.html#process_process_hrtime_time).
  53. ##### keepDecimalsOnWholeSeconds
  54. Type: `boolean`\
  55. Default: `false`
  56. Keep milliseconds on whole seconds: `13s``13.0s`.
  57. Useful when you are showing a number of seconds spent on an operation and don't want the width of the output to change when hitting a whole number.
  58. ##### compact
  59. Type: `boolean`\
  60. Default: `false`
  61. Only show the first unit: `1h 10m``1h`.
  62. Also ensures that `millisecondsDecimalDigits` and `secondsDecimalDigits` are both set to `0`.
  63. ##### unitCount
  64. Type: `number`\
  65. Default: `Infinity`
  66. Number of units to show. Setting `compact` to `true` overrides this option.
  67. ##### verbose
  68. Type: `boolean`\
  69. Default: `false`
  70. Use full-length units: `5h 1m 45s``5 hours 1 minute 45 seconds`
  71. ##### separateMilliseconds
  72. Type: `boolean`\
  73. Default: `false`
  74. Show milliseconds separately. This means they won't be included in the decimal part of the seconds.
  75. ##### formatSubMilliseconds
  76. Type: `boolean`\
  77. Default: `false`
  78. Show microseconds and nanoseconds.
  79. ##### colonNotation
  80. Type: `boolean`\
  81. Default: `false`
  82. Display time using colon notation: `5h 1m 45s``5:01:45`. Always shows time in at least minutes: `1s``0:01`
  83. Useful when you want to display time without the time units, similar to a digital watch.
  84. Setting `colonNotation` to `true` overrides the following options to `false`:
  85. - `compact`
  86. - `formatSubMilliseconds`
  87. - `separateMilliseconds`
  88. - `verbose`
  89. ##### hideYear
  90. Type: `boolean`\
  91. Default: `false`
  92. Hides the year and shows the hidden year additionally as days (365 per year): `1y 3d 5h 1m 45s``368d 5h 1m 45s`.
  93. ##### hideYearAndDays
  94. Type: `boolean`\
  95. Default: `false`
  96. Hides the year and days and shows the hidden values additionally as hours: `1y 3d 5h 1m 45s``8837h 1m 45s`.
  97. ##### hideSeconds
  98. Type: `boolean`\
  99. Default: `false`
  100. Hides the seconds: `1y 3d 5h 1m 45s``1y 3d 5h 1m`.
  101. ##### subSecondsAsDecimals
  102. Type: `boolean`\
  103. Default: `false`
  104. Show sub-second values as decimal seconds: `900ms``0.9s`.
  105. Useful for progress indicators where you want consistent unit format to prevent flickering.
  106. ## Related
  107. - [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module
  108. - [parse-ms](https://github.com/sindresorhus/parse-ms) - Parse milliseconds into an object
  109. - [to-milliseconds](https://github.com/sindresorhus/to-milliseconds) - Convert an object of time properties to milliseconds
  110. - [pretty-bytes](https://github.com/sindresorhus/pretty-bytes) - Convert bytes to a human readable string