All Projects → EvanHahn → Humanizeduration.js

EvanHahn / Humanizeduration.js

Licence: unlicense
361000 becomes "6 minutes, 1 second"

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Humanizeduration.js

Swift-ISO8601-DurationParser
Swift ISO8601 Parser
Stars: ✭ 24 (-98.06%)
Mutual labels:  duration, time
Ruby Duration
Immutable type that represents some amount of time with accuracy in seconds.
Stars: ✭ 122 (-90.11%)
Mutual labels:  time, duration
Durafmt
🕗 Better time duration formatting in Go!
Stars: ✭ 362 (-70.66%)
Mutual labels:  time, duration
As Duration
Extraction of ActiveSupport::Duration from Rails
Stars: ✭ 126 (-89.79%)
Mutual labels:  time, duration
duration-humanizer
361000 becomes "6 minutes, 1 second"
Stars: ✭ 61 (-95.06%)
Mutual labels:  duration, time
Time.dart
⏰ Type-safe DateTime and Duration calculations, powered by extensions.
Stars: ✭ 363 (-70.58%)
Mutual labels:  time, duration
Time
Windows tool for measuring command/program execution speed
Stars: ✭ 21 (-98.3%)
Mutual labels:  time, duration
Pretty Time
Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds.
Stars: ✭ 44 (-96.43%)
Mutual labels:  time
Truetime Android
Android NTP time library. Get the true current time impervious to device clock time changes
Stars: ✭ 1,134 (-8.1%)
Mutual labels:  time
Covid19 Brazil Timeseries
Data collection to analyze the dissemination of COVID-19 through Brazilian states. Contributions are welcome.
Stars: ✭ 43 (-96.52%)
Mutual labels:  time
Period
Complex period comparisons
Stars: ✭ 1,001 (-18.88%)
Mutual labels:  time
Nova Time Field
Laravel Nova Time Field
Stars: ✭ 45 (-96.35%)
Mutual labels:  time
Pert
A simple command line (bash/shell) utility to estimate tasks using PERT [Program Evaluation and Review Technique]
Stars: ✭ 66 (-94.65%)
Mutual labels:  time
Tempo
A Kotlin library for Android to get the current time from multiple sources: SNTP, GPS; or your own time source.
Stars: ✭ 44 (-96.43%)
Mutual labels:  time
Chronos
A standalone DateTime library originally based off of Carbon
Stars: ✭ 1,175 (-4.78%)
Mutual labels:  time
Pickadate.js
The mobile-friendly, responsive, and lightweight jQuery date & time input picker.
Stars: ✭ 7,753 (+528.28%)
Mutual labels:  time
Csnackbar
This is a wrapper for android Snackbar. Which giving support to change Snackbar color, duration, message or even it's content view with a custom view.
Stars: ✭ 76 (-93.84%)
Mutual labels:  duration
Iso8601
Ruby parser to work with ISO8601 dateTimes and durations — http://en.wikipedia.org/wiki/ISO_8601
Stars: ✭ 70 (-94.33%)
Mutual labels:  time
Posix tz db
Generates POSIX timezones strings
Stars: ✭ 57 (-95.38%)
Mutual labels:  time
Timex
A test-friendly replacement for golang's time package
Stars: ✭ 53 (-95.71%)
Mutual labels:  time

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>

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); // '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).

humanizeDuration(3000, { language: "es" }); // '3 segundos'
humanizeDuration(5000, { language: "ko" }); // '5 초'

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"] }); // '3 seconds'
humanizeDuration(3000, {
  language: "bad language",
  fallbacks: ["bad language", "es"],
}); // '3 segundos'

delimiter

String to display between the previous unit and the next value.

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

spacer

String to display between each value and unit.

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

largest

Number representing the maximum number of units to display for the duration.

humanizeDuration(1000000000000); // '31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds'
humanizeDuration(1000000000000, { largest: 2 }); // '31 years, 8 months'

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"] }); // '1 hour'
humanizeDuration(3600000, { units: ["m"] }); // '60 minutes'
humanizeDuration(3600000, { units: ["d", "h"] }); // '1 hour'

round

Boolean value. Use true to round the smallest unit displayed (can be combined with largest and units).

humanizeDuration(1200); // '1.2 seconds'
humanizeDuration(1200, { round: true }); // '1 second'
humanizeDuration(1600, { round: true }); // '2 seconds'

decimal

String to substitute for the decimal point in a decimal fraction.

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.

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

Number that defines a maximal decimal points for float values.

humanizeDuration(8123.456789); // 8.12 seconds
humanizeDuration(8123.456789, { maxDecimalPoints: 3 }); // 8.123 seconds
humanizeDuration(8123.456789, { maxDecimalPoints: 6 }); // 8.123456 seconds
humanizeDuration(8123.45, { maxDecimalPoints: 6 }); // 8.12345 seconds
humanizeDuration(8000, { maxDecimalPoints: 6 }); // 8 seconds

unitMeasures

Customize the value used to calculate each unit of time.

humanizeDuration(400); // '0.4 seconds'
humanizeDuration(400, {
  unitMeasures: {
    y: 365,
    mo: 30,
    w: 7,
    d: 1,
  },
}); // '1 year, 1 month, 5 days'

Combined example

humanizeDuration(3602000, {
  language: "es",
  round: true,
  spacer: " glorioso ",
  units: ["m"],
}); // '60 glorioso minutos'

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
Arabic ar
Bulgarian bg
Catalan ca
Chinese, simplified zh_CN
Chinese, traditional zh_TW
Croatian hr
Czech cs
Danish da
Dutch nl
English en
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
Korean ko
Lao lo
Latvian lv
Lithuanian lt
Malay ms
Norwegian no
Polish pl
Portuguese pt
Romanian ro
Russian ru
Slovak sk
Slovenian sl
Spanish es
Swahili sw
Swedish sv
Thai th
Turkish tr
Ukrainian uk
Urdu ur
Vietnamese vi

For a list of supported languages, you can use the getSupportedLanguages function.

humanizeDuration.getSupportedLanguages();
// ['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.

Credits

Lovingly made by Evan Hahn with help from:

Licensed under the permissive Unlicense. Enjoy!

Related modules

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].