What is human-interval?
The human-interval npm package allows you to parse human-readable time intervals into milliseconds. This can be useful for setting timeouts, scheduling tasks, or any other scenario where you need to convert a human-friendly time format into a machine-readable one.
What are human-interval's main functionalities?
Parse human-readable intervals
This feature allows you to convert a human-readable time interval (e.g., '3 minutes and 30 seconds') into milliseconds. This is useful for applications that need to work with precise time intervals but want to allow users to input times in a more natural format.
const humanInterval = require('human-interval');
const milliseconds = humanInterval('3 minutes and 30 seconds');
console.log(milliseconds); // 210000
Support for various time units
The package supports a wide range of time units including seconds, minutes, hours, days, weeks, months, and years. This allows for flexible and comprehensive time interval parsing.
const humanInterval = require('human-interval');
const milliseconds = humanInterval('2 days, 4 hours, and 20 minutes');
console.log(milliseconds); // 187200000
Combining multiple intervals
You can combine multiple time intervals in a single string, and the package will correctly parse the total time in milliseconds. This is useful for more complex scheduling needs.
const humanInterval = require('human-interval');
const milliseconds = humanInterval('1 hour and 45 minutes');
console.log(milliseconds); // 6300000
Other packages similar to human-interval
ms
The 'ms' package is a simple utility for converting various time formats to milliseconds. It supports a more limited set of time units compared to human-interval but is very lightweight and easy to use. It is ideal for simpler use cases where you don't need the full range of time units supported by human-interval.
parse-duration
The 'parse-duration' package is another utility for parsing human-readable time durations into milliseconds. It supports a wide range of time units and is similar in functionality to human-interval. However, it also includes additional features like parsing negative durations and handling edge cases more gracefully.
moment-duration-format
The 'moment-duration-format' package extends the Moment.js library to support formatting and parsing of durations. While it offers more advanced formatting options compared to human-interval, it requires Moment.js as a dependency, which can add to the package size.
Human Interval
Human readable interval parser for Node.js/the Browser.
Heavily inspired by
matthewmueller/date.
Example Usage
var humanInterval = require('human-interval');
setTimeout(function() {
}, humanInterval('three minutes'));
More sophisticated examples
humanInterval understands all of the following examples:
humanInterval('one minute');
humanInterval('1.5 minutes');
humanInterval('3 days and 4 hours');
humanInterval('3 days, 4 hours and 36 seconds');
The full list
Supported Units
Human Interval supports the following units
seconds
minutes
hours
days
weeks
months
-- assumes 30 daysyears
-- assumes 365 days
Wordy Numbers
Human Interval supports numbers up to ten being written out in English. If you
want to extend it, you can do so by adding more keys to the language map.
Alternatively you could add support for alternative languages.
var humanInterval = require('human-interval');
humanInterval.languageMap['one-hundred'] = 100
humanInterval('one-hundred and fifty seconds')