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 Javascript.
Converts words written in English to numbers by using node-numbered.
Originally inspired by matthewmueller/date.
Uses
Human Interval is used by job scheduling libraries such as Agenda and Bree. They are a job schedulers for Node.js with cron expression syntax, human-friendly times, Dates, and more!
Example usage
const humanInterval = require('human-interval');
setTimeout(() => {
}, humanInterval('three minutes'));
More sophisticated examples
Human Interval understands all of the following examples:
humanInterval('minute');
humanInterval('one minute');
humanInterval('1.5 minutes');
humanInterval('3 days and 4 hours');
humanInterval('3 days, 4 hours and 36 seconds');
humanInterval('4 months, 3 days, 5 hours and forty-five seconds');
The full list
Units
Supports the following units in the plural and singular forms:
seconds
minutes
hours
days
weeks
months
— assumes 30 daysyears
— assumes 365 days
Wordy numbers
Supports numbers being written out in English words.
humanInterval('five minutes');
Hyphenated numbers
Supports hyphenated numbers.
humanInterval('twenty-five seconds');
Negative numbers
Supports negative numbers if the time starts with a -
symbol immediately followed by a number.
humanInterval('-2 minutes');
License
The MIT License