Added format milliseconds method
Some checks failed
Build / Build NPM Project (push) Successful in 58s
Build / Publish Documentation (push) Failing after 4s
Build / Tag Version (push) Successful in 8s

This commit is contained in:
2025-07-25 19:42:01 -04:00
parent b3223661dd
commit b473ade178
2 changed files with 19 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.26.16",
"version": "0.26.17",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -43,6 +43,24 @@ export function formatBytes(bytes: number, decimals = 2) {
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
}
/**
* Convert milliseconds to human-readable duration
* @param {string} number milliseconds
* @return {string} formated duration
*/
export function formatMs(number: string): string {
const ms = parseInt(number, 10);
if (isNaN(ms) || ms < 0) return "Invalid input";
const seconds = ms / 1000;
const minutes = seconds / 60;
const hours = minutes / 60;
const days = hours / 24;
if (days >= 1) return `${days.toFixed(1)} days`;
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
else return `${seconds.toFixed(1)} seconds`;
}
/**
* Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
*