human-duration
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

human-duration Build Status

human-duration formats a duration as a human-readable duration, like 2d 6h. There are other utilities similar to this, but this one aims to be compact (standing at 600 bytes), entirely configurable, and usable with internationalization.

Installation

npm install --save human-duration

Usage

See the test cases for quick usage examples.

const duration = require('human-duration')
 
duration.fmt(second * 1.2) // => '1s'
duration.fmt(second * 90) // => '1m 30s'
duration.fmt(minute * 70 + second * 13) // => '1h 10m 13s'
duration.fmt(hour * 30 + minute * 70 + second * 13) // => '1d 7h 10m 13s'
 
duration.fmt(hour * 30).segments(2) // => '1d 7h'
duration.fmt(hour * 30).separator(':') // => '1d:7h:0m:0s'
duration.fmt(hour * 30).grading([duration.minutes, duration.hour]) // => '0m 30h'
duration.fmt(1040)
  .grading([
    { unit: '% seconds', milliseconds: 1000 },
    { unit: x => x === 1 ? 'a jiffy' : `${x} jiffies}`, milliseconds: 5 }
  ])
  .separator(' and ') // => '1s and 8 jiffies'

Package Sidebar

Install

npm i human-duration

Weekly Downloads

799

Version

1.0.1

License

MIT

Last publish

Collaborators

  • mcph