Git Product home page Git Product logo

parse-duration's Introduction

parse-duration Build Status

convert a human readable duration to ms

Installation

NPM

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, including unicode:

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

The output format can also be defined

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.