const format = {} /**@dateTimeFliter 转换格林日期时间格式为常用日期格式 * @time[必填] Date 格林日期格式 * @part[可选,默认:0] Number 选择返回日期时间部分 列:0:返回所有 1:只返回日期 2:只返回时间 * @dateComplete[可选,默认:true] Boolean 日期位数不足是否添0补齐:true:补齐,false:不补齐 * @timeComplete[可选,默认:true] Boolean 时间位数不足是否添0补齐:true:补齐,false:不补齐 * @dateConnector[可选,默认:-] String 年月日连接符 例: - : / * @timeConnector[可选,默认::] String 时间连接符 例: - : / * @hour12[可选,默认:false] Boolean 是否返回12小时制时间 例: true:返回12小时制时间 false:返回24小时制时间 * @return '2019-11-25 15:05:54' String 返回示例 * **/ format.dateTimeFliter = function(time, part = 0, dateComplete = true, timeComplete = true, dateConnector = '-', timeConnector = ':', hour12 = false) { let year = time.getFullYear() let month = time.getMonth() + 1 let day = time.getDate() let hour = time.getHours() let minute = time.getMinutes() let second = time.getSeconds() let dateStr = '' let timeStr = '' //转换日期 if (dateComplete) { //添0补齐 if (month < 10) { month = '0' + month } if (day < 10) { day = '0' + day } } dateStr = year + dateConnector + month + dateConnector + day //转换时间 //修改小时制 if (hour12) { if (hour > 12) { hour = hour - 12 if (timeComplete) { if (hour < 10) { hour = '下午 ' + '0' + hour } else { hour = '下午 ' + hour } } } else { if (timeComplete) { if (hour < 10) { hour = '上午 ' + '0' + hour } else { hour = '上午 ' + hour } } } } //判断分钟与秒 if (timeComplete) { //添0补齐 if (minute < 10) { minute = '0' + minute } if (second < 10) { second = '0' + second } } timeStr = hour + timeConnector + minute + timeConnector + second //合成输出值 if (part == 0) { return dateStr + ' ' + timeStr } else if (part == 1) { return dateStr } else if (part == 2) { return timeStr } return '传参有误' } function formatInt(int) { return int < 10 ? `0${int}` : int } format.formatDuration = function(duration) { if (duration < 60) { return `00:00:${formatInt(duration)}` } if (duration < 60 * 60) { const min = parseInt(duration / 60) const sec = duration - min * 60 return `00:${formatInt(min)}:${formatInt(sec)}` } const hour = parseInt(duration / (60 * 60)) const remainder = duration - hour * (60 * 60) const min = parseInt(remainder / 60) const sec = remainder - min * 60 return `${formatInt(hour)}:${formatInt(min)}:${formatInt(sec)}` } export default format