Humanize Duration
ES6 version of the humanize-duration library with some modifications.
Converts the time in milliseconds to something like "30 minutes" or "3 days, 1 hour".
Basic usage
This package is available as humanize-duration-es6 on npm.
; const h = localeEn;h;
Usage
By default, library will humanize down to the second, and will return a decimal for the smallest unit.
h // '3 seconds'h // '2.25 seconds'h // '1 day, 3 hours, 2 minutes'
Options
You can change the settings by passing options as the second argument:
delimiter
String to display between the previous unit and the next value.
h // '6 hours and 9 minutes'h // '6 hours--9 minutes'
spacer
String to display between each value and unit.
h // '3 whole days, 14 whole minutes'h // '3days, 14minutes'
largest
Number representing the maximum number of units to display for the duration.
h // '31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds'h // '31 years, 8 month'
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']
h // '1 hour'h // '60 minutes'h // '1 hour'
round
Boolean value. Use true
to round the smallest unit displayed (can be combined with largest
and units
).
h // '1.2 seconds'h // '1 second'h // '2 seconds'
decimal
String to substitute for the decimal point in a decimal fraction.
h // '1.2 seconds'h // '1 point 2 seconds'
conjunction
String to include before the final unit. You can also set serialComma
to false
to eliminate the final comma.
h // '6 hours and 9 minutes'h // '6 hours, 9 minutes, and 1 second'h // '6 hours and 9 minutes'h // '6 hours, 9 minutes and 1 second'
unitMeasures
Customize the value used to calculate each unit of time.
h // '0.4 seconds'h
Combined example
h// '60 great minutes'
Languages
You can also add new languages. For example:
; const shortEnLocale = "y" "mo" "w" "d" "h" "m" "s" "ms" ".";const shortEnglishHumanizer = shortEnLocale spacer: "" delimiter: " "; shortEnglishHumanizer // '4 h, 20 m'
Supported languages
Humanize Duration supports the following languages:
Language | Code |
---|---|
Arabic | ar |
Catalan | ca |
Chinese, simplified | zh_CN |
Chinese, traditional | zh_TW |
Czech | cs |
Danish | da |
Dutch | nl |
English | en |
Finnish | fi |
French | fr |
German | de |
Greek | gr |
Hungarian | hu |
Icelandic | is |
Indonesian | id |
Italian | it |
Japanese | ja |
Korean | ko |
Lithuanian | lt |
Malay | ms |
Norwegian | no |
Polish | pl |
Portuguese | pt |
Russian | ru |
Spanish | es |
Swedish | sv |
Turkish | tr |
Ukrainian | uk |
Vietnamese | vi |
Credits
Based on library humanize-duration
Licensed under the permissive Unlicense.