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.
require (like in Node or with common build systems):
const humanizeDuration =// '12 seconds'
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.
// '3 seconds'// '2.25 seconds'// '1 day, 3 hours, 2 minutes'
You can change the settings by passing options as the second argument:
// '3 segundos'// '5 초'
// '3 seconds'// '3 segundos'
String to display between the previous unit and the next value.
// '6 hours and 9 minutes'// '6 hours--9 minutes'
String to display between each value and unit.
// '3 whole days, 14 whole minutes'// '3days, 14minutes'
Number representing the maximum number of units to display for the duration.
// '31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds'// '31 years, 8 month'
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']
// '1 hour'// '60 minutes'// '1 hour'
Boolean value. Use
true to round the smallest unit displayed (can be combined with
// '1.2 seconds'// '1 second'// '2 seconds'
String to substitute for the decimal point in a decimal fraction.
// '1.2 seconds'// '1 point 2 seconds'
String to include before the final unit. You can also set
false to eliminate the final comma.
// '6 hours and 9 minutes'// '6 hours, 9 minutes, and 1 second'// '6 hours and 9 minutes'// '6 hours, 9 minutes and 1 second'
Number that defines a maximal decimal points for float values.
// 8.12 seconds// 8.123 seconds// 8.123456 seconds// 8.12345 seconds// 8 seconds
Customize the value used to calculate each unit of time.
// '0.4 seconds'
// '60 glorioso minutos'
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// '2 años, 3 meses, 2 días'// '823 días, 19.5 horas'
You can also add new languages to humanizers. For example:
const shortEnglishHumanizer = humanizeDuration// '4 h, 20 m'
You can also add languages after initializing:
const humanizer = humanizeDurationhumanizerlanguagesshortEn ='y'// ...
Internally, the main
humanizeDuration function is just a wrapper around a humanizer.
Humanize Duration supports the following languages:
For a list of supported languages, you can use the
humanizeDuration// ['ar', 'bg', 'ca', 'cs', da', 'de', ...]
This function won't return any new languages you define; it will only return the defaults supported by the library.
Lovingly made by Evan Hahn with help from:
- Martin Prins for language support
- Filipi Siqueira for Portuguese support
- Peter Rekdal Sunde for Norwegian support
- Michał Janiec for Polish support
- Eileen Li for Chinese support
- Tommy Brunn for Swedish support
- Giovanni Pellerano for Italian support
- Rahma Sghaier for Arabic support
- Evgenios Kastanias for Greek support
- Oleksii Mylotskyi for Ukrainian support
- Patrik Simek for Czech support
- Toni Helminen for Finnish support
- Vidmantas Drasutis for Lithuanian support
- Manh Tuan for Vietnamese support
- Leonard Lee for Indonesian & Malay support
- Jesse Jackson for documentation help
- Óli Tómas Freysson for Icelandic support
- Saeed Ganji for Farsi/Persian support
- Caner Elci for Bulgarian support
- Matej Kolesár for Slovak support
- Abdul Jalil for Urdu support
- Wasuwat Limsuparhat for Thai support
- Malikoun for Lao support
- Villu Orav for Estonian support
- Harijs Deksnis for Latvian support
- Nirmala Thapa(Subit) for Faroese support
Licensed under the permissive Unlicense. Enjoy!