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}` } // 设置时间文案 const setDuration = (s) => { let t = ''; s = Math.floor(s); if (s > -1) { let min = Math.floor(s / 60) % 60; let sec = s % 60; if (min < 10) { t += "0"; } t += min + ":"; if (sec < 10) { t += "0"; } t += sec; } return t } /** * 将时间戳(1570550400)格式转为 yyyy-MM-dd格式 */ //时间戳转时间 function formatDate(time, flag) { if (!time) { return } var time = time * 1 var t = new Date(time); var tf = function (i) { return (i < 10 ? '0' : '') + i }; var year = t.getFullYear(); var month = tf(t.getMonth() + 1); var day = tf(t.getDate()); var hour = tf(t.getHours()); var minute = tf(t.getMinutes()); if (flag == 1) { return year + '-' + month + '-' + day + '' + ' ' + hour + ':' + minute; } else if (flag == 2) { return year + '.' + month + '.' + day } else if (flag == 3) { return month + '-' + day + ' ' + hour + ':' + minute; } else if (flag == 4) { return year + '年' + month + '月' + day + '日'; } else if (flag == 5) { return year + '-' + month + '-' + day; } else if (flag == 5) { return year + '-' + month + '-' + day; } else if (flag == 6) { return year + '-' + month; } } module.exports = { formatDate, formatTime, setDuration }