chomens-bot-js/util/secondToHms.js

18 lines
551 B
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/**
* from codegrepper
2022-11-15 21:33:16 -05:00
* @param {Number} d seconds
* @return {String} X hour, X minute, X second
2022-08-14 05:51:45 -04:00
*/
2022-11-27 02:35:28 -05:00
function secondsToHms (d) {
d = Number(d)
const h = Math.floor(d / 3600)
const m = Math.floor(d % 3600 / 60)
const s = Math.floor(d % 3600 % 60)
const hDisplay = h > 0 ? h + (h === 1 ? ' hour, ' : ' hours, ') : ''
const mDisplay = m > 0 ? m + (m === 1 ? ' minute, ' : ' minutes, ') : ''
const sDisplay = s > 0 ? s + (s === 1 ? ' second' : ' seconds') : ''
return hDisplay + mDisplay + sDisplay
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
module.exports = { secondsToHms }