Socket
Socket
Sign inDemoInstall

parse-duration

Package Overview
Dependencies
0
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-duration

convert a human readable duration string to ms


Version published
Weekly downloads
302K
increased by1.84%
Maintainers
1
Created
Weekly downloads
 

Package description

What is parse-duration?

The parse-duration npm package is a utility for parsing human-readable duration strings into milliseconds. It is useful for converting time durations specified in various formats (like '2h', '1d', '5m', etc.) into a standard numerical format that can be used programmatically.

What are parse-duration's main functionalities?

Parse human-readable duration strings

This feature allows you to convert a human-readable duration string (e.g., '2h' for 2 hours) into milliseconds. The code sample demonstrates parsing '2h' into 7200000 milliseconds.

const parseDuration = require('parse-duration');
const duration = parseDuration('2h');
console.log(duration); // 7200000

Parse complex duration strings

This feature allows you to parse more complex duration strings that include multiple time units. The code sample shows how '1d 2h 30m' is parsed into 93600000 milliseconds.

const parseDuration = require('parse-duration');
const duration = parseDuration('1d 2h 30m');
console.log(duration); // 93600000

Handle invalid input gracefully

This feature ensures that invalid input strings are handled gracefully by returning null. The code sample demonstrates parsing an invalid string 'invalid', which results in null.

const parseDuration = require('parse-duration');
const duration = parseDuration('invalid');
console.log(duration); // null

Other packages similar to parse-duration

Readme

Source

parse-duration

convert a human readable duration to ms

Installation

With your favourite package manager:

  • packin: packin add parse-duration
  • component: component install jkroso/parse-duration
  • npm: npm install parse-duration

then in your app:

var parse = require('parse-duration')

API

parse(str)

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

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

Keywords

FAQs

Package last updated on 06 Jun 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc