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 formatChatTime = 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].map(formatNumber).join(':')}` } const getDateArr = 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, hour, minute, second] } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } const getTimeAgo = stringTime =>{ var minute = 1000 * 60; var hour = minute *60; var day = hour *24; var week = day * 7; var month = day * 30; var time1 = new Date().getTime();//当前的时间戳 var time2 = stringTime*1000;//指定时间的时间戳 var time = time1 - time2; var result = null; if(time < 0){ return '刚刚'; }else if(time/month >= 1){ result = formatChatTime(new Date(time2)); }else if(time/week >= 1){ result = formatChatTime(new Date(time2)); }else if(time/day >= 1){ result = formatChatTime(new Date(time2)); }else if(time/hour >= 1){ result = parseInt(time/hour) + "小时前"; }else if(time/minute >= 1){ result = parseInt(time/minute) + "分钟前"; }else { result = "刚刚"; } return result; } module.exports = { formatTime, getDateArr, formatNumber, getTimeAgo, }