What is humanize-duration?
The humanize-duration npm package is used to convert durations, given in milliseconds, into human-readable strings. It is useful for displaying time durations in a more understandable format for users.
What are humanize-duration's main functionalities?
Basic Usage
This feature allows you to convert a duration in milliseconds to a human-readable string. In this example, 3000 milliseconds is converted to '3 seconds'.
const humanizeDuration = require('humanize-duration');
console.log(humanizeDuration(3000)); // '3 seconds'
Custom Units
This feature allows you to specify custom units for the duration. In this example, 3600000 milliseconds is converted to '1 hour' using hours and minutes as units.
const humanizeDuration = require('humanize-duration');
console.log(humanizeDuration(3600000, { units: ['h', 'm'] })); // '1 hour'
Language Support
This feature allows you to specify the language for the output string. In this example, 3000 milliseconds is converted to '3 segundos' in Spanish.
const humanizeDuration = require('humanize-duration');
console.log(humanizeDuration(3000, { language: 'es' })); // '3 segundos'
Round
This feature allows you to round the duration to the nearest unit. In this example, 1234567 milliseconds is rounded to '20 minutes'.
const humanizeDuration = require('humanize-duration');
console.log(humanizeDuration(1234567, { round: true })); // '20 minutes'
Largest
This feature allows you to limit the number of units in the output string. In this example, 1234567890 milliseconds is converted to '14 days, 6 hours' with a limit of 2 units.
const humanizeDuration = require('humanize-duration');
console.log(humanizeDuration(1234567890, { largest: 2 })); // '14 days, 6 hours'
Other packages similar to humanize-duration
moment
Moment.js is a comprehensive library for parsing, validating, manipulating, and formatting dates and times in JavaScript. It includes functionality for humanizing durations, but it is more heavyweight compared to humanize-duration.
date-fns
date-fns is a modern JavaScript date utility library that provides a variety of functions for working with dates, including humanizing durations. It is modular and tree-shakeable, making it a lighter alternative to Moment.js.
luxon
Luxon is a modern JavaScript library for working with dates and times, created by one of the Moment.js developers. It offers a more modern API and includes features for humanizing durations, similar to humanize-duration.
Humanize Duration
I have the time in milliseconds and I want it to become "30 minutes" or "3 days, 1 hour". Enter Humanize Duration!
Basic usage
This package is available as humanize-duration in Node and Bower. You can also include the JavaScript in the browser.
In the browser:
<script src="humanize-duration.js"></script>
<script>
humanizeDuration(12000);
</script>
In Node or Browserify:
var humanizeDuration = require("humanize-duration");
humanizeDuration(12000);
Usage
By default, Humanize Duration will humanize down to the second, and will return a decimal for the smallest unit. It will humanize in English by default.
humanizeDuration(3000)
humanizeDuration(2015)
humanizeDuration(97320000)
You can change the settings by passing options as the second argument:
humanizeDuration(3000, { language: "es" })
humanizeDuration(5000, { language: "ko" })
humanizeDuration(22140000, { delimiter: " and " })
humanizeDuration(22140000, { delimiter: "--" })
humanizeDuration(3600000, { units: ["hours"] })
humanizeDuration(3600000, { units: ["days", "hours"] })
humanizeDuration(3600000, { units: ["minutes"] })
humanizeDuration(3600000, {
language: "es",
units: ["minutes"]
})
Humanizers
If you find yourself setting same options over and over again, you can create a humanizer that changes the defaults, which you can still override later.
var spanishHumanizer = humanizeDuration.humanizer({
language: "es",
units: ["years", "months", "days"]
});
spanishHumanizer(71177400000)
spanishHumanizer(71177400000, { units: ["days", "hours"] })
Internally, the main humanizeDuration
function is just a wrapper around a humanizer.
Supported languages
Humanize Duration supports the following languages:
- Catalan (ca)
- Chinese, simplified (zh-CN)
- Chinese, traditional (zh-TW)
- Danish (da)
- Dutch (nl)
- English (en)
- French (fr)
- German (de)
- Korean (ko)
- Norwegian (nob)
- Polish (pl)
- Portuguese (pt)
- Russian (ru)
- Spanish (es)
Credits
Lovingly made by Evan Hahn with language support by Martin Prins. Thanks to Filipi Siqueira for Portuguese support, Peter Rekdal Sunde for Norwegian support, Michał Janiec for Polish support, and Eileen Li for Chinese support.
Licensed under the WTFPL, so you can do whatever you want. Enjoy!