parse-duration

1.1.1last stable release 2 months ago
Complexity Score
Low
Open Issues
6
Dependent Projects
176
Weekly Downloadsglobal
390,093

License

  • MIT
    • Yesattribution
    • Permissivelinking
    • Permissivedistribution
    • Permissivemodification
    • Nopatent grant
    • Yesprivate use
    • Permissivesublicensing
    • Notrademark grant

Downloads

Readme

parse-duration

convert a human readable duration to ms

Installation

then in your app:

import parse from 'parse-duration'

or CommonJS:

var parse = require('parse-duration')

API

parse(str, format=’ms’)

Convert str to ms

var ns = parse('1ns') // => 1 / 1e6
var μs = parse('1μs') // => 1 / 1000
var ms = parse('1ms') // => 1
var s = parse('1s')   // => ms * 1000
var m = parse('1m')   // => s * 60
var h = parse('1h')   // => m * 60
var d = parse('1d')   // => h * 24
var w = parse('1w')   // => d * 7
var y = parse('1y')   // => d * 365.25

It can also handle basic compound expressions

parse('1hr 20mins') // => 1 * h + 20 * m
parse('1 hr 20 mins') // => 1 * h + 20 * m

youtube format

parse('1h20m0s') // => 1 * h + 20 * m

comma seperated numbers

parse('27,681 ns') // => 27681 * ns

And most other types of noise

parse('running length: 1hour:20mins') // => 1 * h + 20 * m

You can even use negatives

parse('-1hr 40mins') // => 1 * h + 40 * m

And exponents

parse('2e3s') // => 2000 * s

Available unit types are:

  • nanoseconds (ns)
  • microseconds (μs)
  • milliseconds (ms)
  • seconds (s, sec)
  • minutes (m, min)
  • hours (h, hr)
  • days (d)
  • weeks (w, wk)
  • months
  • years (y, yr)

And its easy to add more, including unicode:

parse['сек'] = parse['sec']
parse('5сек') // => 5000

The output format can also be defined

parse('1hr 20mins', 'm') // => 80

Dependencies

No runtime dependency information found for this package.

CVE IssuesActive
0
Scorecards Score
3.80
Test Coverage
No Data
Follows Semver
Yes
Github Stars
243
Dependenciestotal
1
DependenciesOutdated
1
DependenciesDeprecated
0
Threat Modelling
No
Repo Audits
No

Learn how to distribute parse-duration in your own private NPM registry

npm config set registry  https://npm.cloudsmith.com/owner/repo
Processing...
Done
npm install parse-duration
Processing...
Done

22 Releases

NPM on Cloudsmith

Getting started with NPM on Cloudsmith is fast and easy.