js-秒转化为时分秒显示

61 阅读1分钟
    function secondsToHMS(seconds, hUnit = '小时', mUnit = '分', sUnit = '秒') {
        const h = Math.floor(seconds / 3600);
        const m = Math.floor((seconds / 60) % 60);
        const s = Math.floor(second % 60);
        let result = `${s}${sUnit}`;
        (m > 0) && (result = `${m}${mUnit}${s}${sUnit}`);
        (h > 0) && (result = `${h}${hUnit}${m}${mUnit}${s}${sUnit}`);
        return result;
    }