/**
* @description 根据时间戳获取指定格式时间
* @param {number} tamp 时间戳
* @param {string} format 时间格式 如:YYYY-MM-DD hh:mm:ss
* @param {boolean} padStart 是否填充两位数字不足补零
* @return {string}
*/
export function getTime(tamp = new Date().getTime(), format, padStart = true) {
let unitArr = ['YYYY', 'MM', 'DD', 'hh', 'mm', 'ss'];
let date = new Date(tamp);
let timeObj = {};
let timeStr = format || 'YYYY-MM-DD hh:mm:ss';
timeObj.YYYY = padStart ? String(date.getFullYear()) : date.getFullYear();
timeObj.MM = padStart
? String(date.getMonth() + 1).padStart(2, '0')
: date.getMonth() + 1;
timeObj.DD = padStart
? String(date.getDate()).padStart(2, '0')
: date.getDate();
timeObj.hh = padStart
? String(date.getHours()).padStart(2, '0')
: date.getHours();
timeObj.mm = padStart
? String(date.getMinutes()).padStart(2, '0')
: date.getMinutes();
timeObj.ss = padStart
? String(date.getSeconds()).padStart(2, '0')
: date.getSeconds();
unitArr.forEach(item => {
timeStr = timeStr.replace(item, timeObj[item]);
});
return timeStr;
}
网友评论