format-duration-time v1.5.4
format-duration-time
This is a package to format duration, written in TypeScript.
Install
npm
npm i format-duration-timeyarn
yarn add format-duration-timeUsage
You can import this package to your code as below in JavaScript and TypeScript.
import duration from 'format-duration-time';or
var duration = require("format-duration-time").defaultThe followings are some sample codes to use this package.
duration(3600000).format('h')// 1
duration(9000000).format('h:m')// 2:30
duration(1, 'h').format('m[minute]ss[second]')//60minute00second
duration(60, 's').format('mm')// 01
duration(1000, 's').format('s', {digitSeparator: ','})// 1,000
duration(1, 'S').format('SSSS')// 0001
duration(90, 'm').format('h', { digitSeparator: ',', decimalPlace: 3, roundType: 'round'})//1.500duration and format methods should be called with following arguments.
duration(value, unit).format(template);To padding zero on the head of formated duration, format function should be called by multiple token.
duration(1, 'S').format('SSSS')// 0001Default input unit is milli second. To escape your token in the template you can use square brackets.
duration(1, 'h').format('m[minute]ss[second]')//60minute00secondAvalable duration unit
| unit argument | |
|---|---|
| Day | d |
| Hour | h |
| Minute | m |
| Second | s |
| Milli second | S(default) |
Avalable format templates
| token | examples | |
|---|---|---|
| Day | d dd ... | 1, 2, 3, ... 01, 02, 03 ... ... |
| Hour | h hh ... | 1, 2, 3, ... 01, 02, 03 ... ... |
| Minute | m mm ... | 1, 2, 3, ... 01, 02, 03, ... ... |
| Second | s ss ... | 1, 2, 3, ... 01, 02, 03, ... ... |
| Milli second | S SS ... | 1, 2, 3, ... 01, 02, 03, ... ... |
To add or subtract duration, you can use add or sub method like following examples.
duration(2, 'm').add(1, 's').format('m [minutes,]s [seconds]')//2 minutes,1 seconds
duration(2, 'm').sub(1, 's').format('m [minutes,]s [seconds]')//1 minutes,59 secondsadd and sub methods does not change the original duration object.
const firstDuration = duration(1);
const secondDuration = firstDuration.add(1);
firstDuration.format('S');// 1
secondDuration.format('S');// 2Options
This is an optional parameter. You can add following options in the format function as Object.
duration(value, unit).format(template, {options})digitSeparator
{ digitSeparator: string }To put digit separator in every 3 digit, add digitSeparator option. Value of digitSeparator must be string.
duration(1000, 's').format('s', {digitSeparator: ','})// 1,000decimalPlace
{ decimalPlace: number }You can set number of decimal digits to display. decimalPlace option will work on only the smallest template token. Value of decimalPlace must be integer.
duration(90, 'm').format('h:m', { decimalPlace: 3})// 1:30.000roundType
{ roundType: 'floor', 'ceil' or 'round' }Set roundType to round the lowest value. The default type is floor.
duration(30, 's').format('m', { roundType: 'round'})//1You can set multiple options like folowing example.
duration(90, 'm').format('h', { digitSeparator: ',', decimalPlace: 3, roundType: 'round'})//1.5006 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago