UNPKG

labo-components

Version:
31 lines (28 loc) 869 B
// create a human readable string like 01:23:45 from the given time in sec // - optionally adds decimals: 01:23:45.67 // - strips empty hours 00:12:34 --> 12:34 export const timeToString = (time, decimals) => { const hours = Math.floor(time / 3600); time -= hours * 3600; const minutes = Math.floor(time / 60); time -= minutes * 60; const seconds = Math.floor(time); time -= seconds; // fix float rounding issues const ms = Math.floor(time * 100); if (ms >= 100) { seconds++; ms -= 100; } return ( // optional hours (hours ? ("0" + hours).substr(-2) + ":" : "") + // minutes ("0" + minutes).substr(-2) + ":" + // seconds ("0" + seconds).substr(-2) + // optionally 2 decimals (decimals ? "." + (ms + "000").substr(0, 2) : "") ); };