/**
* @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
}
网友评论