human-time
Prints milliseconds time for human reading. Very customizable. Designed for use with Node.js and the browser.
Installation
To install it on Node.js:
npm install custom-human-time
Require in Node.js:
var HumanTime = ;
In the Browser
Usage
var humanTime = ; humanTime; // => "1 second"humanTime; // => "2 seconds"humanTime; // => "2 minutes"humanTime; // => "5 seconds"humanTime // => "45 years" (2015-1970)
Options
The constructor accepts an options parameter.
names
is an object that can be used to print the time units in other format than the default. Can be used for appending "ago" or for using another language. The character`
(backtick) is used to indicate where the plural goes. For example:
// In spanish:var humanTime = names: millisecond: ' milisegundo`' second: ' segundo`' minute: ' minuto`' hour: ' hora`' day: ' día`' week: ' semana`' month: ' mes`' year: ' año`'; // " time ago"var humanTime = names: millisecond: ' millisecond` ago' second: ' second` ago' minute: ' minute` ago' hour: ' hour` ago' day: ' day` ago' week: ' week` ago' month: ' month` ago' year: ' year` ago'; // time unitsvar humanTime = names: millisecond: 'ms' second: 's' minute: 'm' hour: 'h' day: 'd' week: 'w' month: 'M' year: 'y';
plurals
is an object that can be used to change the letters of any name. In the above example, the plural for "mes" is "meses". So, to fix that, use the code below:
var humanTime = names: millisecond: ' milisegundo`' second: ' segundo`' minute: ' minuto`' hour: ' hora`' day: ' día`' week: ' semana`' month: ' mes`' year: ' año`' plurals: month: 'es' ;
digits
indicates how many digits to print after the decimal point. By default it isundefined
.
var humanTime = digits: 2;humanTime; // "1.23 seconds"
round
is the function used to "round" the number. For example, if you want to use precision or truncate:
// Precisionvar { return +num;};var humanTime = digits: 3 round: precisionRound;humanTime; // => "54.3 seconds" // Truncatevar { return num >> 0;};var humanTime = round: truncateRound;humanTime; // => "1 second"
All these options can be modified after using the constructor.
var humanTime = ;humanTimedigits = 1;humanTime // => "1.2 seconds"