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
ms
The 'ms' package is another utility for parsing and formatting milliseconds. It can convert human-readable strings into milliseconds and vice versa. Compared to parse-duration, 'ms' is more focused on simplicity and has a smaller feature set.
moment-duration-format
The 'moment-duration-format' package extends Moment.js to format durations. It is more feature-rich compared to parse-duration, offering advanced formatting options and integration with Moment.js for more complex date and time manipulations.
humanize-duration
The 'humanize-duration' package is designed to convert milliseconds into human-readable strings. It is essentially the reverse of parse-duration, focusing on converting numerical durations into readable formats.
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')
var μs = parse('1μs')
var ms = parse('1ms')
var s = parse('1s')
var m = parse('1m')
var h = parse('1h')
var d = parse('1d')
var w = parse('1w')
var y = parse('1y')
It can also handle basic compound expressions
parse('1hr 20mins')
parse('1 hr 20 mins')
youtube format
parse('1h20m0s')
comma seperated numbers
parse('27,681 ns')
And most other types of noise
parse('running length: 1hour:20mins')
You can even use negatives
parse('2hr -40mins')
And exponents
parse('2e3s')
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сек')
The output format can also be defined
parse('1hr 20mins', 'm')