const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } /** * 数据格式化 * @param {*} date 时间或者时间字符串 * @param {*} format 时间格式 * @returns 格式化后的时间字符串 */ function dateFormat(time, format = 'h小时m分s秒') { console.log(time) var hours = parseInt((time / (1000 * 60 * 60))); var minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60)); var seconds = parseInt((time % (1000 * 60)) / 1000); return `${hours}小时${minutes}分${seconds}秒` } function dateFormat2(time, format = 'h小时m分s秒') { var day = parseInt((time) / (1000 * 60 * 60 * 24)); var hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60)); var seconds = parseInt((time % (1000 * 60)) / 1000); return `${day}天${hours}小时${minutes}分${seconds}秒` } module.exports = { formatTime: formatTime, dateFormat: dateFormat, dateFormat2: dateFormat2, }