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 } //时间戳转时间 function formatDate(time, flag) { const t = new Date(time); const tf = function(i){return (i < 10 ? '0' : '') + i}; const year = t.getFullYear(); const month = tf(t.getMonth() + 1); const day = tf(t.getDate()); const hour = tf(t.getHours()); const minute = tf(t.getMinutes()); //console.log( month + '月' + day + '日' + hour + ':' + minute); if(flag == 1) { return month + '月' + day + '日' + ' ' + hour + ':' + minute; }else if(flag == 2) { return year + '-' + month + '-' + day }else if(flag == 3){ return month + '-' + day + ' ' + hour + ':' + minute; } } module.exports = { formatTime: formatTime, formatDate }