parse-duration
convert a human readable duration string to a duration format
Last updated 8 months ago by dy .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install parse-duration 
SYNC missed versions from official npm registry.

parse-duration Build Status

convert a human readable duration to ms

Installation

npm install parse-duration

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

whitespace

parse('1 hr 20 mins') // => 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('2hr -40mins') // => 1 * h + 20 * 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

The output format can also be defined

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

Current Tags

  • 0.4.4                                ...           latest (8 months ago)

15 Versions

  • 0.4.4                                ...           8 months ago
  • 0.4.3                                ...           9 months ago
  • 0.4.2                                ...           9 months ago
  • 0.4.1                                ...           9 months ago
  • 0.4.0                                ...           9 months ago
  • 0.3.3                                ...           9 months ago
  • 0.3.2                                ...           9 months ago
  • 0.3.1                                ...           9 months ago
  • 0.3.0                                ...           9 months ago
  • 0.2.1                                ...           9 months ago
  • 0.2.0                                ...           9 months ago
  • 0.1.3                                ...           9 months ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Downloads
Today 3
This Week 3
This Month 710
Last Day 16
Last Week 220
Last Month 992
Dependencies (0)
None
Dev Dependencies (1)
Dependents (168)

Copyright 2014 - 2017 © taobao.org |