美文网首页
2022-10-21

2022-10-21

作者: 乐宝呗 | 来源:发表于2022-10-21 14:33 被阅读0次

    1. 根据总秒数转换指定格式时间(倒计时)

    /**
     * @description 根据总秒数转换指定格式时间
     * @param {number} second 秒
     * @param {string} format 时间格式 如:YY-MM-DD hh:mm:ss
     * @param {boolean} padStart 是否填充两位数字不足补零
     * @return {string}
     */
    export function calcTime(second, format, padStart = true) {
        let unitArr = [
            { name: 'YY', unit: 12 * 30 * 24 * 60 * 60 * 1 },
            { name: 'MM', unit: 30 * 24 * 60 * 60 * 1 },
            { name: 'DD', unit: 24 * 60 * 60 * 1 },
            { name: 'hh', unit: 60 * 60 * 1 },
            { name: 'mm', unit: 60 * 1 },
            { name: 'ss', unit: 1 },
        ];
        let _format = format || 'hh:mm:ss';
        let _second = second;
        let timeStr = '';
        unitArr.forEach(item => {
            let arr = _format.split(item.name);
            if (arr.length === 2) {
                let count = String(Math.floor(_second / item.unit));
                padStart && (count = count.padStart(2, '0'));
                timeStr += arr[0] + count;
                _second = _second - count * item.unit;
                _format = arr[1];
            }
        });
        timeStr += _format;
        return timeStr
    }
    

    摘抄自: https://www.jianshu.com/p/c588ddf5a0ee

    相关文章

      网友评论

          本文标题:2022-10-21

          本文链接:https://www.haomeiwen.com/subject/dtkmzrtx.html