Added format milliseconds method
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.18",
|
"version": "0.26.19",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -46,18 +46,19 @@ export function formatBytes(bytes: number, decimals = 2) {
|
|||||||
/**
|
/**
|
||||||
* Convert milliseconds to human-readable duration
|
* Convert milliseconds to human-readable duration
|
||||||
* @param {string} ms milliseconds
|
* @param {string} ms milliseconds
|
||||||
|
* @param {boolean} short Use unit initial instead of word
|
||||||
* @return {string} formated duration
|
* @return {string} formated duration
|
||||||
*/
|
*/
|
||||||
export function formatMs(ms: number): string {
|
export function formatMs(ms: number, short = false): string {
|
||||||
if (isNaN(ms) || ms < 0) return "Invalid input";
|
if (isNaN(ms) || ms < 0) return "Invalid input";
|
||||||
const seconds = ms / 1000;
|
const seconds = ms / 1000;
|
||||||
const minutes = seconds / 60;
|
const minutes = seconds / 60;
|
||||||
const hours = minutes / 60;
|
const hours = minutes / 60;
|
||||||
const days = hours / 24;
|
const days = hours / 24;
|
||||||
if (days >= 1) return `${days.toFixed(1)} days`;
|
if (days >= 1) return `${days.toFixed(1)} ${short ? 'd' : 'days'}`;
|
||||||
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
|
else if (hours >= 1) return `${hours.toFixed(1)} ${short ? 'h' : 'hours'}`;
|
||||||
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
|
else if (minutes >= 1) return `${minutes.toFixed(1)} ${short ? 'm' : 'minutes'}`;
|
||||||
else return `${seconds.toFixed(1)} seconds`;
|
else return `${seconds.toFixed(1)} ${short ? 's' : 'seconds'}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user