npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

format-duration-time

v1.5.4

Published

This is a package to format duration.

Downloads

557

Readme

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