duration-pretty
v0.1.1
Published
A pure JavaScript library that parses duration time length to format display
Downloads
100
Readme
Duration Pretty
The plugin is a pure JavaScript library that parses duration time length to format display.
Getting Started
Installation
npm install --save duration-pretty
Documentation
The duration(timestamp, type)
get two parameters: timestamp && type.
timestamp: number
type: seconds | milliseconds
Format duration time using a template string to format()
.
var { duration } = require('duration-pretty')
duration(7380, 'seconds').format('H:mm') // "2:03"
duration(36610000333, 'milliseconds').format('Y:MM:DD:HH:mm:ss:SSS') // "1:01:28:17:26:40:333"
or using ES6 Module:
import { duration } from 'duration-pretty'
duration(7380, 'seconds').format('H:mm') // "2:03"
The template string is parsed for universal token characters, which are replaced with the duration's value for each unit type. The tokens are:
years: Y
months: M | MM
days: D | DD
hours: H | HH
minutes: m | mm
seconds: s | ss
milliseconds: SSS
Escape token characters within the template string using []
.
duration(3661, 'seconds').format('H [[H]], m [[m] countdown]') // "1 [H], 1 [m countdown]"
Tests
You can find all cases in files:/test/*.spec.js
, And testing Using below script.
npm run test