@spa-tools/utilities
humanizeMs()
The humanizeMs()
function converts a number of milliseconds into the largest applicable
unit of either year, month, week, day, minute, or second.
Usage
import { humanizeMs } from '@spa-tools/utilities';
const humanizedMs1 = humanizeMs(1000);
console.log(humanizedMs1);
// --> [1, 'second']
const humanizedMs2 = humanizeMs(1000 * 60 * 60);
console.log(humanizedMs2);
// --> [1, 'hour']
const humanizedMs3 = humanizeMs(1000 * 60 * 60 * 24 * 365);
console.log(humanizedMs3);
// --> [1, 'year']
Arguments
Name | Type | Required? | Default | Description |
---|---|---|---|---|
milliseconds | number | yes | - | The milliseconds to humanize |
decimalPlaces | number | no | 1 | The decimal places to round/format to |
Returns
Returns two-element tuple as follows:
- element 1 at index 0 =
number
representing the converted-to value with requested decimal places - element 2 at index 1 =
string
representing the converted-to unit