From fb077775b6f5d3889c154e944185d30899c4d638 Mon Sep 17 00:00:00 2001 From: ztimson Date: Fri, 25 Jul 2025 19:51:06 -0400 Subject: [PATCH] Added format milliseconds method --- package.json | 2 +- src/string.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index f31c190..2a86b63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.26.18", + "version": "0.26.19", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/string.ts b/src/string.ts index 825340b..6c769c5 100644 --- a/src/string.ts +++ b/src/string.ts @@ -46,18 +46,19 @@ export function formatBytes(bytes: number, decimals = 2) { /** * Convert milliseconds to human-readable duration * @param {string} ms milliseconds + * @param {boolean} short Use unit initial instead of word * @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"; 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`; + if (days >= 1) return `${days.toFixed(1)} ${short ? 'd' : 'days'}`; + else if (hours >= 1) return `${hours.toFixed(1)} ${short ? 'h' : 'hours'}`; + else if (minutes >= 1) return `${minutes.toFixed(1)} ${short ? 'm' : 'minutes'}`; + else return `${seconds.toFixed(1)} ${short ? 's' : 'seconds'}`; } /**