f-duration
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Convert a number in milliseconds to a standard duration string.

Input 😞 Value 😂
999 '0:00'
1000 * 60 '1:00'
1000 * 60 * 60 '1:00:00'
1000 * 60 * 60 * 24 '1:00:00:00'

Install

Get it on npm

npm install f-duration

Get it on yarn

yarn add  f-duration

Usage

import format from 'f-duration'
format(ms: number, options?: IOption)
import format from 'f-duration'

// anything under a second is rounded down to zero
format(999) // '0:00'

// 1000 milliseconds is a second
format(1000) // '0:01'

// 1999 rounds down to 0:01
format(1000 * 2 - 1) // '0:01'

// 60 seconds is a minute
format(1000 * 60) // '1:00'

// 59 seconds looks like this
format(1000 * 60 - 1) // '0:59'

// 60 minutes is an hour
format(1000 * 60 * 60) // '1:00:00'

// 59 minutes and 59 seconds looks like this
format(1000 * 60 * 60 - 1) // '59:59'

// 24 hours is a day
format(1000 * 60 * 60 * 24) // '1:00:00:00'

// 23 hours, 59 minutes, and 59 seconds looks like this
format(1000 * 60 * 60 * 24 - 1) // '23:59:59'

// 365 days looks like this (not bothering with years)
format(1000 * 60 * 60 * 24 * 365) // '365:00:00:00'

// anything under a second is rounded down to zero
format(-999) // '0:00'

// 1000 milliseconds is a second
format(-1000) // '-0:01'

// 365 days looks like this (not bothering with years)
format(-1000 * 60 * 60 * 24 * 365) // '-365:00:00:00'

// with `leading` option, formatting looks like this
format(1000 * 60, { leading: true }) // '01:00'
format(1000 * 60 - 1, { leading: true }) // '00:59'
format(1000 * 60 * 60, { leading: true }) // '01:00:00'

Default Options

Name Type Default Description
leading boolean false leading zero eg:(format(1000,{leading:true})) (00:01) instead of (0:01)

License

MIT License

Package Sidebar

Install

npm i f-duration

Weekly Downloads

10

Version

1.0.1

License

MIT

Unpacked Size

17.9 kB

Total Files

18

Last publish

Collaborators

  • tal7aouy