Gathering detailed insights and metrics for humanize-duration
Gathering detailed insights and metrics for humanize-duration
Gathering detailed insights and metrics for humanize-duration
Gathering detailed insights and metrics for humanize-duration
361000 becomes "6 minutes, 1 second"
npm install humanize-duration
99.8
Supply Chain
100
Quality
77.8
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,656 Stars
723 Commits
175 Forks
13 Watching
4 Branches
60 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.8%
222,659
Compared to previous day
Last week
2.6%
1,207,620
Compared to previous week
Last month
13%
5,029,592
Compared to previous month
Last year
33.2%
53,559,143
Compared to previous year
7
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.
This package is available as humanize-duration on npm and Bower. You can also include the JavaScript file in the browser.
1npm install humanize-duration
With require
(like in Node or with common build systems):
1const humanizeDuration = require("humanize-duration"); 2humanizeDuration(12000); 3// => "12 seconds"
With a <script>
tag:
1<script src="humanize-duration.js"></script> 2<script> 3 humanizeDuration(12000); 4</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.
1humanizeDuration(3000); 2// => "3 seconds" 3 4humanizeDuration(2250); 5// => "2.25 seconds" 6 7humanizeDuration(97320000); 8// => "1 day, 3 hours, 2 minutes"
You can change the settings by passing options as the second argument.
Language for unit display. Accepts an ISO 639-1 code from one of the supported languages.
Default: "en"
.
1humanizeDuration(3000, { language: "es" }); 2// => "3 segundos" 3 4humanizeDuration(5000, { language: "ko" }); 5// => "5 ì´ˆ"
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: []
.
1humanizeDuration(3000, { 2 language: "bad language", 3 fallbacks: ["en"], 4}); 5// => "3 seconds" 6 7humanizeDuration(3000, { 8 language: "bad language", 9 fallbacks: ["another bad language", "es"], 10}); 11// => "3 segundos"
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"]
1humanizeDuration(69000, { units: ["h", "m", "s", "ms"] }); 2// => "1 minute, 9 seconds" 3 4humanizeDuration(3600000, { units: ["h"] }); 5// => "1 hour" 6 7humanizeDuration(3600000, { units: ["m"] }); 8// => "60 minutes" 9 10humanizeDuration(3600000, { units: ["d", "h"] }); 11// => "1 hour"
Integer representing the maximum number of units to use.
Default: Infinity
1humanizeDuration(1000000000000); 2// => "31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds" 3 4humanizeDuration(1000000000000, { largest: 2 }); 5// => "31 years, 8 months"
A boolean that, if true
, rounds the smallest unit.
Default: false
1humanizeDuration(1200); 2// => "1.2 seconds" 3 4humanizeDuration(1200, { round: true }); 5// => "1 second" 6 7humanizeDuration(1600, { round: true }); 8// => "2 seconds"
String to display between units.
Default: ", "
in most languages, " ï» "
for Arabic
1humanizeDuration(22140000); 2// => "6 hours, 9 minutes" 3 4humanizeDuration(22140000, { delimiter: " and " }); 5// => "6 hours and 9 minutes"
String to display between the count and the word.
Default: " "
1humanizeDuration(260040000); 2// => "3 days, 14 minutes" 3 4humanizeDuration(260040000, { spacer: " whole " }); 5// => "3 whole days, 14 whole minutes"
String to display between the integer and decimal parts of a count, if relevant.
Default depends on the language.
1humanizeDuration(1200); 2// => "1.2 seconds" 3 4humanizeDuration(1200, { decimal: " point " }); 5// => "1 point 2 seconds"
String to include before the final unit.
You can also set serialComma
to false
to eliminate the final comma.
Default: ""
1humanizeDuration(22140000, { conjunction: " and " }); 2// => "6 hours and 9 minutes" 3 4humanizeDuration(22141000, { conjunction: " and " }); 5// => "6 hours, 9 minutes, and 1 second" 6 7humanizeDuration(22140000, { conjunction: " and ", serialComma: false }); 8// => "6 hours and 9 minutes" 9 10humanizeDuration(22141000, { conjunction: " and ", serialComma: false }); 11// => "6 hours, 9 minutes and 1 second"
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
1humanizeDuration(8123.456789); 2// => "8.123456789 seconds" 3 4humanizeDuration(8123.456789, { maxDecimalPoints: 3 }); 5// => "8.123 seconds" 6 7humanizeDuration(8100, { maxDecimalPoints: 99 }); 8// => "8.1 seconds" 9 10humanizeDuration(8000, { maxDecimalPoints: 99 }); 11// => "8 seconds" 12 13humanizeDuration(7999, { maxDecimalPoints: 2 }); 14// => "7.99 seconds"
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
1humanizeDuration(1234); 2// => "1.234 seconds" 3 4humanizeDuration(1234, { 5 digitReplacements: [ 6 "Zero", 7 "One", 8 "Two", 9 "Three", 10 "Four", 11 "Five", 12 "Six", 13 "Seven", 14 "Eight", 15 "Nine", 16 ], 17}); 18// => "One.TwoThreeFour seconds"
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 }
1humanizeDuration(2629800000); 2// => "1 month" 3 4humanizeDuration(2629800000, { 5 unitMeasures: { 6 y: 31557600000, 7 mo: 30 * 86400000, 8 w: 604800000, 9 d: 86400000, 10 h: 3600000, 11 m: 60000, 12 s: 1000, 13 ms: 1, 14 }, 15}); 16// => "1 month, 10 hours, 30 minutes"
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.
1const spanishHumanizer = humanizeDuration.humanizer({ 2 language: "es", 3 units: ["y", "mo", "d"], 4}); 5 6spanishHumanizer(71177400000); 7// => "2 años, 3 meses, 2 dÃas" 8 9spanishHumanizer(71177400000, { units: ["d", "h"] }); 10// => "823 dÃas, 19.5 horas"
You can also add new languages to humanizers. For example:
1const shortEnglishHumanizer = humanizeDuration.humanizer({ 2 language: "shortEn", 3 languages: { 4 shortEn: { 5 y: () => "y", 6 mo: () => "mo", 7 w: () => "w", 8 d: () => "d", 9 h: () => "h", 10 m: () => "m", 11 s: () => "s", 12 ms: () => "ms", 13 }, 14 }, 15}); 16 17shortEnglishHumanizer(15600000); 18// => "4 h, 20 m"
You can also add languages after initializing:
1const humanizer = humanizeDuration.humanizer(); 2 3humanizer.languages.shortEn = { 4 y: () => "y", 5 // ...
Internally, the main humanizeDuration
function is just a wrapper around a humanizer.
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.
1humanizeDuration.getSupportedLanguages(); 2// => ["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.
Lovingly made by Evan Hahn with help from:
Licensed under the permissive Unlicense. Enjoy!
timeAdverb
option1d 2h 3m 4s
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More@types/humanize-duration
TypeScript definitions for humanize-duration
humanize-duration-ts
An implementation of Humanize Duration in typescript
ngx-humanize-duration
[![npm version](https://badge.fury.io/js/ngx-humanize-duration.svg)](https://badge.fury.io/js/ngx-humanize-duration) ![Build](https://github.com/george3447/ngx-humanize-duration/workflows/Build/badge.svg?branch=master)
humanize-duration-es6
ES6 library to convert millisecond durations to human-readable text.