Skip to content
Formats a duration as a human-readable string
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Init Mar 12, 2017
.npmignore Init Mar 12, 2017
.travis.yml
LICENSE Init Mar 12, 2017
index.ts
package.json
readme.md
test.js
tsconfig.json

readme.md

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.

Note: duration.fmt takes milliseconds.

const duration = require('human-duration')

const second = 1000
const minute = second * 60
const hour = minute * 60

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'
You can’t perform that action at this time.