Quantcast
Channel: JavaScript seconds to time string with format hh:mm:ss - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by artnikpro for JavaScript seconds to time string with format hh:mm:ss

$
0
0
/** * Formats seconds (number) to H:i:s format. * 00:12:00 * * When "short" option is set to true, will return: * 0:50 * 2:00 * 12:00 * 1:00:24 * 10:00:00 */export default function formatTimeHIS (seconds, { short = false } = {}) {  const pad = num => num < 10 ? `0${num}` : num  const H = pad(Math.floor(seconds / 3600))  const i = pad(Math.floor(seconds % 3600 / 60))  const s = pad(seconds % 60)  if (short) {    let result = ''    if (H > 0) result += `${+H}:`    result += `${H > 0 ? i : +i}:${s}`    return result  } else {    return `${H}:${i}:${s}`  }}

Viewing all articles
Browse latest Browse all 49

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>