Humanize Duration
I have the time in milliseconds and I want it to become "30 minutes" or "3 days, 1 hour". Enter Humanize Duration!
This library is actively maintained but no new features will be added.
Installation
This package is available as humanize-duration on npm and Bower. You can also include the JavaScript file in the browser.
npm install humanize-duration
Basic usage
With require
(like in Node or with common build systems):
const humanizeDuration = require("humanize-duration");
humanizeDuration(12000);
With a <script>
tag:
<script src="humanize-duration.js"></script>
<script>
humanizeDuration(12000);
</script>
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(2250);
humanizeDuration(97320000);
Options
You can change the settings by passing options as the second argument:
language
Language for unit display (accepts an ISO 639-1 code from one of the supported languages).
humanizeDuration(3000, { language: "es" });
humanizeDuration(5000, { language: "ko" });
fallbacks
Fallback languages if the provided language cannot be found (accepts an ISO 639-1 code from one of the supported languages). It works from left to right.
humanizeDuration(3000, { language: "bad language", fallbacks: ["en"] });
humanizeDuration(3000, {
language: "bad language",
fallbacks: ["bad language", "es"],
});
delimiter
String to display between the previous unit and the next value.
humanizeDuration(22140000, { delimiter: " and " });
humanizeDuration(22140000, { delimiter: "--" });
spacer
String to display between each value and unit.
humanizeDuration(260040000, { spacer: " whole " });
humanizeDuration(260040000, { spacer: "" });
largest
Number representing the maximum number of units to display for the duration.
humanizeDuration(1000000000000);
humanizeDuration(1000000000000, { largest: 2 });
units
Array of strings to define which units are used to display the duration (if needed). Can be one, or a combination of any, of the following: ['y', 'mo', 'w', 'd', 'h', 'm', 's', 'ms']
humanizeDuration(3600000, { units: ["h"] });
humanizeDuration(3600000, { units: ["m"] });
humanizeDuration(3600000, { units: ["d", "h"] });
round
Boolean value. Use true
to round the smallest unit displayed (can be combined with largest
and units
).
humanizeDuration(1200);
humanizeDuration(1200, { round: true });
humanizeDuration(1600, { round: true });
decimal
String to substitute for the decimal point in a decimal fraction.
humanizeDuration(1200);
humanizeDuration(1200, { decimal: " point " });
conjunction
String to include before the final unit. You can also set serialComma
to false
to eliminate the final comma.
humanizeDuration(22140000, { conjunction: " and " });
humanizeDuration(22141000, { conjunction: " and " });
humanizeDuration(22140000, { conjunction: " and ", serialComma: false });
humanizeDuration(22141000, { conjunction: " and ", serialComma: false });
maxDecimalPoints
Number that defines a maximal decimal points for float values.
humanizeDuration(8123.456789);
humanizeDuration(8123.456789, { maxDecimalPoints: 3 });
humanizeDuration(8123.456789, { maxDecimalPoints: 6 });
humanizeDuration(8123.45, { maxDecimalPoints: 6 });
humanizeDuration(8000, { maxDecimalPoints: 6 });
unitMeasures
Customize the value used to calculate each unit of time.
humanizeDuration(400);
humanizeDuration(400, {
unitMeasures: {
y: 365,
mo: 30,
w: 7,
d: 1,
},
});
Combined example
humanizeDuration(3602000, {
language: "es",
round: true,
spacer: " glorioso ",
units: ["m"],
});
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.
const spanishHumanizer = humanizeDuration.humanizer({
language: "es",
units: ["y", "mo", "d"],
});
spanishHumanizer(71177400000);
spanishHumanizer(71177400000, { units: ["d", "h"] });
You can also add new languages to humanizers. For example:
const shortEnglishHumanizer = humanizeDuration.humanizer({
language: "shortEn",
languages: {
shortEn: {
y: () => "y",
mo: () => "mo",
w: () => "w",
d: () => "d",
h: () => "h",
m: () => "m",
s: () => "s",
ms: () => "ms",
},
},
});
shortEnglishHumanizer(15600000);
You can also add languages after initializing:
const humanizer = humanizeDuration.humanizer()
humanizer.languages.shortEn = {
y: () => 'y',
Internally, the main humanizeDuration
function is just a wrapper around a humanizer.
Supported languages
Humanize Duration supports the following languages:
Language | Code |
---|
Afrikaans | af |
Albanian | sq |
Arabic | ar |
Basque | eu |
Bengali | bn |
Bulgarian | bg |
Catalan | ca |
Chinese, simplified | zh_CN |
Chinese, traditional | zh_TW |
Croatian | hr |
Czech | cs |
Danish | da |
Dutch | nl |
English | en |
Esperanto | eo |
Estonian | et |
Faroese | fo |
Farsi/Persian | fa |
Finnish | fi |
French | fr |
German | de |
Greek | el |
Hebrew | he |
Hindi | hi |
Hungarian | hu |
Icelandic | is |
Indonesian | id |
Italian | it |
Japanese | ja |
Kannada | kn |
Khmer | km |
Korean | ko |
Kurdish | ku |
Lao | lo |
Latvian | lv |
Lithuanian | lt |
Macedonian | mk |
Malay | ms |
Marathi | mr |
Norwegian | no |
Polish | pl |
Portuguese | pt |
Romanian | ro |
Russian | ru |
Serbian | sr |
Slovak | sk |
Slovenian | sl |
Spanish | es |
Swahili | sw |
Swedish | sv |
Tamil | ta |
Telugu | te |
Thai | th |
Turkish | tr |
Ukrainian | uk |
Urdu | ur |
Vietnamese | vi |
Welsh | cy |
For a list of supported languages, you can use the getSupportedLanguages
function. The results may not be in the same order every time.
humanizeDuration.getSupportedLanguages();
This function won't return any new languages you define; it will only return the defaults supported by the library.
Credits
Lovingly made by Evan Hahn with help from:
Licensed under the permissive Unlicense. Enjoy!
Related modules