Format TimeUnit Util Function

May 17, 2023

Util Functions

Format TimeUnit Util Function

Thuta Sann

Thuta Sann

In this section, i will demonstrate how to convert seconds to the formatted Time Unit


Share This Snippet To :

This util function will convert the seconds (number) to the formatted Time Unit string.
/** * Format Time Unit * @param { number } minutes * @returns { string } Formatted Time Unit */ export function formatTimeUnit(minutes) { const hours = Math.floor(minutes / 60); const remainingMinutes = minutes % 60; const ampm = hours < 12 ? 'AM' : 'PM'; let hour12 = hours % 12; if (hour12 === 0) { hour12 = 12; } return `${hour12}:${String(remainingMinutes).padStart(2, '0')} ${ampm}`; } // Usage const ftu = formatTimeUnit(3000) console.log('ftu', ftu) // 2:00 PM

Cookie

I baked some cookies that you have to accept, if you want to enjoy my portfolio.
In order to gather information and make improvements, I should use some third-party cookies too, Can I?