Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

humanize-duration

EvanHahn3.7mUnlicense3.32.1TypeScript support: definitely-typed

Convert millisecond durations to English and many other languages.

humanize, duration, time, hours, minutes, seconds, days, years, months

readme

Humanize Duration

npm version

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);
// => "12 seconds"

With a <script> tag:

<script src="humanize-duration.js"></script>
<script>
  humanizeDuration(12000);
</script>

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);
// => "3 seconds"

humanizeDuration(2250);
// => "2.25 seconds"

humanizeDuration(97320000);
// => "1 day, 3 hours, 2 minutes"

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.

Default: "en".

humanizeDuration(3000, { language: "es" });
// => "3 segundos"

humanizeDuration(5000, { language: "ko" });
// => "5 초"

fallbacks

Array of fallback languages, if the provided language cannot be found. Like language, accepts an ISO 639-1 code from one of the supported languages. It works from left to right, choosing the first language that's found.

Default: [].

humanizeDuration(3000, {
  language: "bad language",
  fallbacks: ["en"],
});
// => "3 seconds"

humanizeDuration(3000, {
  language: "bad language",
  fallbacks: ["another bad language", "es"],
});
// => "3 segundos"

units

Array of possible units to use. Units are y, mo, w, d, h, m, s, and ms.

Units are skipped if their count is zero. For example, if you pass a duration of 1000 and units ["h", "m", "s"], the output will be "1 second".

Must be in descending order of unit size. For example, ["h", "m"] is valid but ["m", "h"] is not.

Default: ["y", "mo", "w", "d", "h", "m", "s"]

humanizeDuration(69000, { units: ["h", "m", "s", "ms"] });
// => "1 minute, 9 seconds"

humanizeDuration(3600000, { units: ["h"] });
// => "1 hour"

humanizeDuration(3600000, { units: ["m"] });
// => "60 minutes"

humanizeDuration(3600000, { units: ["d", "h"] });
// => "1 hour"

largest

Integer representing the maximum number of units to use.

Default: Infinity

humanizeDuration(1000000000000);
// => "31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds"

humanizeDuration(1000000000000, { largest: 2 });
// => "31 years, 8 months"

round

A boolean that, if true, rounds the smallest unit.

Default: false

humanizeDuration(1200);
// => "1.2 seconds"

humanizeDuration(1200, { round: true });
// => "1 second"

humanizeDuration(1600, { round: true });
// => "2 seconds"

delimiter

String to display between units.

Default: ", " in most languages, " ﻭ " for Arabic

humanizeDuration(22140000);
// => "6 hours, 9 minutes"

humanizeDuration(22140000, { delimiter: " and " });
// => "6 hours and 9 minutes"

spacer

String to display between the count and the word.

Default: " "

humanizeDuration(260040000);
// => "3 days, 14 minutes"

humanizeDuration(260040000, { spacer: " whole " });
// => "3 whole days, 14 whole minutes"

decimal

String to display between the integer and decimal parts of a count, if relevant.

Default depends on the language.

humanizeDuration(1200);
// => "1.2 seconds"

humanizeDuration(1200, { decimal: " point " });
// => "1 point 2 seconds"

conjunction

String to include before the final unit.

You can also set serialComma to false to eliminate the final comma.

Default: ""

humanizeDuration(22140000, { conjunction: " and " });
// => "6 hours and 9 minutes"

humanizeDuration(22141000, { conjunction: " and " });
// => "6 hours, 9 minutes, and 1 second"

humanizeDuration(22140000, { conjunction: " and ", serialComma: false });
// => "6 hours and 9 minutes"

humanizeDuration(22141000, { conjunction: " and ", serialComma: false });
// => "6 hours, 9 minutes and 1 second"

maxDecimalPoints

Integer that defines the maximum number of decimal points to show, if relevant. If undefined, the count will be converted to a string using Number.prototype.toString().

This does not round any numbers. See the round option.

Default: undefined

humanizeDuration(8123.456789);
// => "8.123456789 seconds"

humanizeDuration(8123.456789, { maxDecimalPoints: 3 });
// => "8.123 seconds"

humanizeDuration(8100, { maxDecimalPoints: 99 });
// => "8.1 seconds"

humanizeDuration(8000, { maxDecimalPoints: 99 });
// => "8 seconds"

humanizeDuration(7999, { maxDecimalPoints: 2 });
// => "7.99 seconds"

digitReplacements

Array of ten strings to which will replace the numerals 0-9. Useful if a language uses different numerals.

Default: undefined for most languages, ["۰", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"] for Arabic

humanizeDuration(1234);
// => "1.234 seconds"

humanizeDuration(1234, {
  digitReplacements: [
    "Zero",
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
  ],
});
// => "One.TwoThreeFour seconds"

unitMeasures

Use this option with care. It is an advanced feature.

Object used to customize the value used to calculate each unit of time. Most useful when you want to update the length of years or months, which have ambiguous lengths.

Default: { y: 31557600000, mo: 2629800000, w: 604800000, d: 86400000, h: 3600000, m: 60000, s: 1000, ms: 1 }

humanizeDuration(2629800000);
// => "1 month"

humanizeDuration(2629800000, {
  unitMeasures: {
    y: 31557600000,
    mo: 30 * 86400000,
    w: 604800000,
    d: 86400000,
    h: 3600000,
    m: 60000,
    s: 1000,
    ms: 1,
  },
});
// => "1 month, 10 hours, 30 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.

const spanishHumanizer = humanizeDuration.humanizer({
  language: "es",
  units: ["y", "mo", "d"],
});

spanishHumanizer(71177400000);
// => "2 años, 3 meses, 2 días"

spanishHumanizer(71177400000, { units: ["d", "h"] });
// => "823 días, 19.5 horas"

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);
// => "4 h, 20 m"

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
Amharic am
Arabic ar
Basque eu
Bengali bn
Bulgarian bg
Catalan ca
Central Kurdish ckb
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
Mongolian mn
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
Uzbek uz
Uzbek (Cyrillic) uz_CYR
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();
// => ["af", "ar", "bg", "bn", "ca", ...]

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!

changelog

3.32.1 / 2024-05-25

  • fix: hide unit count if 2 in Arabic (see #222)

3.32.0 / 2024-03-29

  • new: Amharic support (am)
  • change: use Object.assign internally on newer runtimes, which should be slightly faster

3.31.0 / 2023-11-10

  • new: Central Kurdish support (ckb)

3.30.0 / 2023-09-17

  • new: Uzbek support (uz and uz_CYR)

3.29.0 / 2023-07-09

  • new: digitReplacements option
  • change: cleaned up documentation
  • change: shrank package size slightly

3.28.0 / 2023-01-14

  • new: Mongolian support (mn)

3.27.3 / 2022-08-26

  • fix: ordering for Swahili (see #207)

3.27.2 / 2022-05-26

  • change: shrank package size slightly

3.27.1 / 2021-12-05

  • fix: Japanese word for "month" (see #203)

3.27.0 / 2021-06-06

  • change: use Arabic numerals, delimiters, and updated words (see #198 and #199)

3.26.0 / 2021-05-03

  • new: Afrikaans support (af)
  • new: Albanian support (sq)
  • new: Basque support (eu)
  • new: Bengali support (bn)
  • new: Esperanto support (eo)
  • new: Kannada support (kn)
  • new: Khmer support (km)
  • new: Kurdish support (ku)
  • new: Macedonian support (mk)
  • new: Marathi support (mr)
  • new: Serbian support (sr)
  • new: Tamil support (ta)
  • new: Telugu support (te)
  • new: Welsh support (cy)
  • fix: invalid syntax (trailing commas) for old browsers

3.25.2 / 2021-04-17

  • fix: Thai word for "week" is now less colloquial (see #196)

3.25.1 / 2021-01-07

  • fix: Slovenian might return undefined for weeks (see #194)

3.25.0 / 2020-12-07

  • new: Slovenian support

3.24.0 / 2020-10-01

  • new: Hindi support

3.23.1 / 2020-06-11

  • change: removed extra check when humanizing Arabic
  • change: simplify logic for Latvian

3.23.0 / 2020-05-21

  • new: Swahili support

3.22.0 / 2020-02-28

  • new: Hebrew support

3.21.0 / 2019-09-26

  • new: Færøsk support

3.20.1 / 2019-07-28

  • fix: if decimal is missing in a language, use . (see #160)

3.20.0 / 2019-07-17

  • new: Latvian support
  • fix: better handle properties that happen to also be on Object.prototype

3.19.0 / 2019-07-08

  • new: Estonian support

3.18.0 / 2019-03-07

  • new: maxDecimalPoints option

3.17.0 / 2019-01-06

  • new: fallbacks option

3.16.0 / 2018-12-01

  • new: Romanian support
  • new: Thai support

3.15.3 / 2018-09-18

  • fix: Greek should be el, not gr

3.15.2 / 2018-09-12

  • fix: corrections for Arabic

3.15.1 / 2018-07-15

  • fix: corrections for Croatian

3.15.0 / 2018-06-12

  • new: Croatian support
  • new: Lao support

3.14.0 / 2018-03-23

  • new: Urdu support

3.13.0 / 2018-03-19

  • new: Slovak support

3.12.1 / 2018-01-03

  • fix: Ukranian translation for weeks was incorrect

3.12.0 / 2017-11-07

  • new: Bulgarian support

3.11.0 / 2017-10-25

  • new: Persian/Farsi support

3.10.1 / 2017-07-21

  • fix: use singular form from -2 to 2 in French

3.10.0 / 2016-11-29

  • new: Icelandic support

3.9.1 / 2016-07-24

  • fix: Russian and Ukranian fixes

3.9.0 / 2016-06-10

  • new: Indonesian support
  • new: Malay support

3.8.0 / 2016-05-19

  • new: conjunctions and serialComma options

  • update: improve documentation

3.7.1 / 2016-04-26

  • fix: rounding had some more errors

3.7.0 / 2016-03-18

  • new: Vietnamese support

3.6.1 / 2016-02-19

  • update: readme should use single quotes

  • fix: rounding had some errors (for example, you could get "1 day, 24 hours")

  • fix: readme example was missing some quotes in its result

3.6.0 / 2016-02-12

  • new: Lithuanian support

  • fix: add trailing semicolon to improve concatenation with other libraries

3.5.0 / 2016-01-13

  • new: Finnish support
  • new: recommend millisec module in readme

3.4.0 / 2015-12-11

  • new: Czech support

  • update: minor performance improvements

  • update: add "related modules" section to readme

  • fix: add Greek to readme

3.3.0 / 2015-08-05

  • new: unitMeasures option

3.2.1 / 2015-07-22

  • fix: document largest option

3.2.0 / 2015-07-15

  • new: Ukrainian support

  • fix: things would break in global strict mode

3.1.0 / 2015-06-25

  • new: Greek support

3.0.0 / 2015-05-29

  • new: largest option to get largest units

  • update: languages can change decimal point (which can be overridden)

  • update: fix some unintuitive round behavior
  • update: specify units in shorthand rather than longhand
  • update: use the Unlicense
  • update: nob is now no
  • update: use underscores instead of dashes for Chinese

  • remove: half units

2.8.0 / 2015-05-10

  • new: getSupportedLanguages

2.7.0 / 2015-05-08

  • new: changelog
  • new: Arabic support
  • new: Italian support

  • remove: (spoken) languages from package.json keywords