1.5.4 • Published 4 years ago

format-duration-time v1.5.4

Weekly downloads
37
License
ISC
Repository
github
Last release
4 years ago

format-duration-time

This is a package to format duration, written in TypeScript. Build Status Codecov Coverage

Install

npm

npm i format-duration-time

yarn

yarn add format-duration-time

Usage

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").default

The 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.500

duration 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')// 0001

Default 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]')//60minute00second

Avalable duration unit

unit argument
Dayd
Hourh
Minutem
Seconds
Milli secondS(default)

Avalable format templates

tokenexamples
Dayd dd ...1, 2, 3, ... 01, 02, 03 ... ...
Hourh hh ...1, 2, 3, ... 01, 02, 03 ... ...
Minutem mm ...1, 2, 3, ... 01, 02, 03, ... ...
Seconds ss ...1, 2, 3, ... 01, 02, 03, ... ...
Milli secondS 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 seconds

add 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');// 2

Options

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,000

decimalPlace

{ 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.000

roundType

{ roundType: 'floor', 'ceil' or 'round' }

Set roundType to round the lowest value. The default type is floor.

duration(30, 's').format('m', { roundType: 'round'})//1

You can set multiple options like folowing example.

duration(90, 'm').format('h', { digitSeparator: ',', decimalPlace: 3, roundType: 'round'})//1.500
1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.10

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago